File: shell_systemd_test.go

package info (click to toggle)
direnv 2.37.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 872 kB
  • sloc: sh: 1,499; csh: 83; makefile: 7
file content (71 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cmd

import (
	"fmt"
	"strings"
	"testing"
)

func StringPtr(value string) *string {
	tmp := value
	return &tmp
}

func TestCutEncapsulated_ok(t *testing.T) {
	input := "\"encapsulated string with \n line return\""
	cutInput, isEncapsulated := cutEncapsulated(input, "\"")
	if !isEncapsulated {
		t.Error("Test TestCutEncapsulated_ok failing.")
	}
	assertEqual(t, "encapsulated string with \n line return", cutInput)
}

func TestExport_ok(t *testing.T) {

	env := Env{
		"Key":  " just a Value",
		"Ex1":  `'single quotes ' works like that'`,
		"Ex2":  `however, you can't use quotes inline`,
		"Ex3":  `double quotes " are doing the "same" way`,
		"Ex4":  `quotes allows escapes: \n \x`,
		"Ex5":  `quotes are doing trick for chars: ", \, $`,
		"Ex6":  `naked values allows escapes: \a, \b, \c`,
		"Ex7":  `and even \$, and even at the beginning`,
		"Ex8":  `\x all the rest`,
		"Ex9":  `in naked values backslash \allows splitting values`,
		"Ex10": `quotes\nallow multi lines`,
		"Ex11": `'with single quotes around it and '' single quotes in it '''`,
		"Ex12": `"with quotes around it and quotes "" in "" it"`,
	}

	systemdExporter := Systemd
	actualOutput, err := env.ToShell(systemdExporter)
	if err != nil {
		t.Fatalf("ToShell() failed: %v", err)
	}

	expectedOutputMap := map[string]string{
		"Key":  " just a Value",
		"Ex1":  "'single quotes \\' works like that'",
		"Ex2":  "\"however, you can't use quotes inline\"",
		"Ex3":  "\"double quotes \\\" are doing the \\\"same\\\" way\"",
		"Ex4":  `"quotes allows escapes: \n \x"`,
		"Ex5":  "\"quotes are doing trick for chars: \\\", \\, $\"",
		"Ex6":  "\"naked values allows escapes: \\a, \\b, \\c\"",
		"Ex7":  "\"and even \\$, and even at the beginning\"",
		"Ex8":  "\"\\x all the rest\"",
		"Ex9":  "\"in naked values backslash \\allows splitting values\"",
		"Ex10": `"quotes\nallow multi lines"`,
		"Ex11": `'with single quotes around it and \'\' single quotes in it \'\''`,
		"Ex12": `"with quotes around it and quotes \"\" in \"\" it"`,
	}
	for key, expectedValue := range expectedOutputMap {
		_, afterPrefix, PrefixFound := strings.Cut(actualOutput, key+"=")
		actualValue, _, SuffixFound := strings.Cut(afterPrefix, "\n")
		if !PrefixFound || !SuffixFound {
			t.Error("Test for systemd shell failed, couldn't not found expected key=value")
		}
		assertEqual(t, fmt.Sprint(expectedValue), fmt.Sprint(actualValue))
	}

}