| 1 | package runscript |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/sylvinus/agent-vm/internal/paths" |
| 11 | ) |
| 12 | |
| 13 | func TestProjectPath(t *testing.T) { |
| 14 | |
| 15 | |
| 16 | for _, win := range []bool{false, true} { |
| 17 | old := paths.Windows |
| 18 | paths.Windows = func() bool { return win } |
| 19 | dir := "/p" |
| 20 | if win { |
| 21 | dir = "C:/p" |
| 22 | } |
| 23 | for v, want := range map[string]string{ |
| 24 | "": dir + "/.agent-vm.runtime.sh", |
| 25 | ".mytool/run.sh": dir + "/.mytool/run.sh", |
| 26 | "/etc/mytool/r.sh": "/etc/mytool/r.sh", |
| 27 | } { |
| 28 | t.Setenv("AGENT_VM_PROJECT_RUNTIME", v) |
| 29 | if got := ProjectPath(dir); got != want { |
| 30 | t.Errorf("windows=%v %q: %q, want %q", win, v, got, want) |
| 31 | } |
| 32 | } |
| 33 | paths.Windows = old |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | |
| 38 | func TestInterpreterAWK(t *testing.T) { |
| 39 | if _, err := exec.LookPath("awk"); err != nil { |
| 40 | t.Skip("no awk") |
| 41 | } |
| 42 | for _, c := range []string{ |
| 43 | "", "echo hi\n", "#!/bin/bash\n", "#!/bin/bash -e\n", "#!/bin/sh\r\n", "#!/usr/bin/env bash\n", |
| 44 | "#!/usr/bin/env -S bash -e\n", "#!/usr/bin/env FOO=1 sh\n", "#!/usr/bin/env\n", "#!env -i\n", "#!\n", |
| 45 | "#! /bin/bash\n", "#!/usr/bin/python3\n", "#!/bin/zsh\n", "#!/bin/bash", "#!/opt/x/env\n", "#!\tsh\n", |
| 46 | "#!/usr/bin/env -S\n", "#!/bin/dash\n", " #!/bin/bash\n", |
| 47 | } { |
| 48 | f := filepath.Join(t.TempDir(), "s") |
| 49 | os.WriteFile(f, []byte(c), 0o644) |
| 50 | out, err := exec.Command("awk", ShebangAWK, f).Output() |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | if got := Interpreter(c); got != strings.TrimSuffix(string(out), "\n") { |
| 55 | t.Errorf("Interpreter(%q) = %q, awk %q", c, got, out) |
| 56 | } |
| 57 | } |
| 58 | } |