|
| 1 | +package lint |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "runtime" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/errata-ai/vale/v3/internal/system" |
| 10 | +) |
| 11 | + |
| 12 | +// write creates an executable script and returns its path. |
| 13 | +func write(t *testing.T, dir, name, body string) string { |
| 14 | + t.Helper() |
| 15 | + |
| 16 | + path := filepath.Join(dir, name) |
| 17 | + if err := os.WriteFile(path, []byte(body), 0o755); err != nil { //nolint:gosec |
| 18 | + t.Fatal(err) |
| 19 | + } |
| 20 | + return path |
| 21 | +} |
| 22 | + |
| 23 | +// The interpreter is read out of a shebang, which is a claim the file makes |
| 24 | +// about itself rather than something Vale can verify. Anything but Python has |
| 25 | +// to come back empty: the caller's next move is to hand it the Docutils server |
| 26 | +// source, and a shell reads that as a script. See #1137. |
| 27 | +func TestRSTInterpreterOnlyNamesPython(t *testing.T) { |
| 28 | + dir := t.TempDir() |
| 29 | + |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + shebang string |
| 33 | + python bool |
| 34 | + }{ |
| 35 | + {"pyenv shim", "#!/usr/bin/env bash\nset -e\nexec pyenv exec rst2html \"$@\"\n", false}, |
| 36 | + {"sh wrapper", "#!/bin/sh\nexec /usr/bin/rst2html \"$@\"\n", false}, |
| 37 | + {"env perl", "#!/usr/bin/env perl\n", false}, |
| 38 | + {"env with no argument", "#!/usr/bin/env\n", false}, |
| 39 | + {"no shebang", "not a script\n", false}, |
| 40 | + {"empty file", "", false}, |
| 41 | + {"absolute python", "#!/usr/bin/python3\n", true}, |
| 42 | + {"versioned python", "#!/usr/local/bin/python3.12\n", true}, |
| 43 | + {"pypy", "#!/usr/bin/pypy3\n", true}, |
| 44 | + } |
| 45 | + |
| 46 | + for _, tt := range tests { |
| 47 | + t.Run(tt.name, func(t *testing.T) { |
| 48 | + got := rstInterpreter(write(t, dir, "rst2html", tt.shebang)) |
| 49 | + |
| 50 | + if !tt.python { |
| 51 | + if got != "" { |
| 52 | + t.Errorf("rstInterpreter = %q; want \"\"", got) |
| 53 | + } |
| 54 | + return |
| 55 | + } |
| 56 | + if !rePython.MatchString(filepath.Base(got)) { |
| 57 | + t.Errorf("rstInterpreter = %q; want a Python", got) |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// `#!/usr/bin/env python3` names the interpreter in the second field, and that |
| 64 | +// field is a bare name -- it has to be resolved on PATH, not returned as-is. |
| 65 | +func TestRSTInterpreterResolvesEnvPython(t *testing.T) { |
| 66 | + if system.Which([]string{"python3", "python"}) == "" { |
| 67 | + t.Skip("no python on PATH") |
| 68 | + } |
| 69 | + |
| 70 | + got := rstInterpreter(write(t, t.TempDir(), "rst2html", "#!/usr/bin/env python3\n")) |
| 71 | + if got != "" && !filepath.IsAbs(got) { |
| 72 | + t.Errorf("rstInterpreter = %q; want an absolute path", got) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// The failure in #1137 was not a wrong answer -- the probe caught that and Vale |
| 77 | +// fell back to spawning rst2html per file, so the linting was correct. It was |
| 78 | +// what running the probe did on the way: `bash -c` on the Docutils source runs |
| 79 | +// its first line, `import sys`, as a shell command, and `import` is |
| 80 | +// ImageMagick's screenshot tool. |
| 81 | +// |
| 82 | +// So this asserts on the side effect, not the return value. `import` here |
| 83 | +// records that it ran. |
| 84 | +func TestRSTProbeRunsNothingThroughAShell(t *testing.T) { |
| 85 | + if runtime.GOOS == "windows" { |
| 86 | + t.Skip("no shebang handling on Windows") |
| 87 | + } else if system.Which([]string{"bash"}) == "" { |
| 88 | + t.Skip("bash not installed") |
| 89 | + } |
| 90 | + |
| 91 | + dir := t.TempDir() |
| 92 | + sentinel := filepath.Join(dir, "ran") |
| 93 | + |
| 94 | + // A wrapper of the shape a version manager installs: a Bourne-Again shell |
| 95 | + // script under the name of the tool it forwards to. |
| 96 | + shim := write(t, dir, "rst2html", "#!/usr/bin/env bash\nset -e\nexit 0\n") |
| 97 | + |
| 98 | + // Every word on the source's first line is a command a shell would try. |
| 99 | + for _, name := range []string{"import", "sys"} { |
| 100 | + write(t, dir, name, "#!/bin/sh\necho ran > "+sentinel+"\n") |
| 101 | + } |
| 102 | + |
| 103 | + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) |
| 104 | + |
| 105 | + rstProbe(shim) |
| 106 | + |
| 107 | + if _, err := os.Stat(sentinel); err == nil { |
| 108 | + t.Error("the Docutils source was executed by a shell") |
| 109 | + } |
| 110 | +} |
0 commit comments