(t *testing.T)
| 48 | } |
| 49 | |
| 50 | func TestMakeDirectories(t *testing.T) { |
| 51 | t.Parallel() |
| 52 | |
| 53 | t.Run("NoPaths", func(t *testing.T) { |
| 54 | assert.Equal(t, |
| 55 | MakeDirectories("/asdf/jklm"), |
| 56 | `test -d '/asdf/jklm'`) |
| 57 | }) |
| 58 | |
| 59 | t.Run("Children", func(t *testing.T) { |
| 60 | assert.DeepEqual(t, |
| 61 | MakeDirectories("/asdf", "/asdf/jklm", "/asdf/qwerty"), |
| 62 | `mkdir -p '/asdf/jklm' '/asdf/qwerty' && { chmod 0775 '/asdf/jklm' '/asdf/qwerty' || :; }`) |
| 63 | }) |
| 64 | |
| 65 | t.Run("Grandchild", func(t *testing.T) { |
| 66 | script := MakeDirectories("/asdf", "/asdf/qwerty/boots") |
| 67 | assert.DeepEqual(t, script, |
| 68 | `mkdir -p '/asdf/qwerty/boots' && { chmod 0775 '/asdf/qwerty/boots' '/asdf/qwerty' || :; }`) |
| 69 | |
| 70 | t.Run("ShellCheckPOSIX", func(t *testing.T) { |
| 71 | shellcheck := require.ShellCheck(t) |
| 72 | |
| 73 | dir := t.TempDir() |
| 74 | file := filepath.Join(dir, "script.sh") |
| 75 | assert.NilError(t, os.WriteFile(file, []byte(script), 0o600)) |
| 76 | |
| 77 | // Expect ShellCheck for "sh" to be happy. |
| 78 | // - https://www.shellcheck.net/wiki/SC2148 |
| 79 | cmd := exec.CommandContext(t.Context(), shellcheck, "--enable=all", "--shell=sh", file) |
| 80 | output, err := cmd.CombinedOutput() |
| 81 | assert.NilError(t, err, "%q\n%s", cmd.Args, output) |
| 82 | }) |
| 83 | }) |
| 84 | |
| 85 | t.Run("Long", func(t *testing.T) { |
| 86 | script := MakeDirectories("/", strings.Repeat("/asdf", 20)) |
| 87 | |
| 88 | t.Run("PrettyYAML", func(t *testing.T) { |
| 89 | b, err := yaml.Marshal(script) |
| 90 | s := string(b) |
| 91 | assert.NilError(t, err) |
| 92 | assert.Assert(t, !strings.HasPrefix(s, `"`) && !strings.HasPrefix(s, `'`), |
| 93 | "expected plain unquoted scalar, got:\n%s", b) |
| 94 | }) |
| 95 | }) |
| 96 | |
| 97 | t.Run("Relative", func(t *testing.T) { |
| 98 | assert.Equal(t, |
| 99 | MakeDirectories("/x", "one", "two/three"), |
| 100 | `mkdir -p '/x/one' '/x/two/three' && { chmod 0775 '/x/one' '/x/two/three' '/x/two' || :; }`, |
| 101 | "expected paths relative to base") |
| 102 | |
| 103 | assert.Equal(t, |
| 104 | MakeDirectories("/x/y/z", "../one", "./two", "../../../../three"), |
| 105 | `mkdir -p '/x/y/one' '/x/y/z/two' '/three' && { chmod 0775 '/x/y/one' '/x/y/z/two' '/three' || :; }`, |
| 106 | "expected paths relative to base") |
| 107 |
nothing calls this directly
no test coverage detected