|
| 1 | +package mapcli |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestInstallScriptExists(t *testing.T) { |
| 11 | + _, err := os.Stat("install.sh") |
| 12 | + if os.IsNotExist(err) { |
| 13 | + t.Fatal("install.sh does not exist") |
| 14 | + } |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("failed to stat install.sh: %v", err) |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +func TestInstallScriptExecutable(t *testing.T) { |
| 21 | + info, err := os.Stat("install.sh") |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("failed to stat install.sh: %v", err) |
| 24 | + } |
| 25 | + |
| 26 | + // Check if executable bit is set for owner |
| 27 | + if info.Mode()&0100 == 0 { |
| 28 | + t.Error("install.sh should be executable") |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestInstallScriptSyntax(t *testing.T) { |
| 33 | + cmd := exec.Command("bash", "-n", "install.sh") |
| 34 | + output, err := cmd.CombinedOutput() |
| 35 | + if err != nil { |
| 36 | + t.Fatalf("install.sh has syntax errors: %v\n%s", err, output) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestInstallScriptContent(t *testing.T) { |
| 41 | + content, err := os.ReadFile("install.sh") |
| 42 | + if err != nil { |
| 43 | + t.Fatalf("failed to read install.sh: %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + script := string(content) |
| 47 | + |
| 48 | + // Check for required elements |
| 49 | + checks := []struct { |
| 50 | + name string |
| 51 | + contains string |
| 52 | + }{ |
| 53 | + {"shebang", "#!/bin/bash"}, |
| 54 | + {"repo reference", "pmarsceill/mapcli"}, |
| 55 | + {"OS detection", "uname -s"}, |
| 56 | + {"arch detection", "uname -m"}, |
| 57 | + {"GitHub API", "api.github.com"}, |
| 58 | + {"install dir", "INSTALL_DIR"}, |
| 59 | + {"error handling", "set -e"}, |
| 60 | + {"linux support", "linux"}, |
| 61 | + {"darwin support", "darwin"}, |
| 62 | + {"amd64 support", "amd64"}, |
| 63 | + {"arm64 support", "arm64"}, |
| 64 | + } |
| 65 | + |
| 66 | + for _, check := range checks { |
| 67 | + t.Run(check.name, func(t *testing.T) { |
| 68 | + if !strings.Contains(script, check.contains) { |
| 69 | + t.Errorf("install.sh should contain %q", check.contains) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestInstallScriptShellcheck(t *testing.T) { |
| 76 | + // Skip if shellcheck is not installed |
| 77 | + _, err := exec.LookPath("shellcheck") |
| 78 | + if err != nil { |
| 79 | + t.Skip("shellcheck not installed") |
| 80 | + } |
| 81 | + |
| 82 | + cmd := exec.Command("shellcheck", "install.sh") |
| 83 | + output, err := cmd.CombinedOutput() |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("shellcheck found issues:\n%s", output) |
| 86 | + } |
| 87 | +} |
0 commit comments