|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package docker |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestCheck_SkipUpdateWidgets(t *testing.T) { |
| 14 | + // This test verifies the SkipUpdateWidgets option is wired through. |
| 15 | + // Since we don't have a real mx binary in CI, we just verify the |
| 16 | + // function returns the expected "mx not found" error. |
| 17 | + opts := CheckOptions{ |
| 18 | + ProjectPath: "/nonexistent/app.mpr", |
| 19 | + SkipUpdateWidgets: true, |
| 20 | + Stdout: &bytes.Buffer{}, |
| 21 | + Stderr: &bytes.Buffer{}, |
| 22 | + } |
| 23 | + |
| 24 | + err := Check(opts) |
| 25 | + if err == nil { |
| 26 | + t.Fatal("expected error when mx binary not found") |
| 27 | + } |
| 28 | + if got := err.Error(); got != "mx not found; specify --mxbuild-path pointing to Mendix installation directory" { |
| 29 | + // Accept any error about mx not being found |
| 30 | + t.Logf("got error: %s", got) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// createFakeMxDir creates a temp directory with fake mx and mxbuild scripts |
| 35 | +// that log their first argument to a file. |
| 36 | +func createFakeMxDir(t *testing.T) (dir, logFile string) { |
| 37 | + t.Helper() |
| 38 | + dir = t.TempDir() |
| 39 | + logFile = filepath.Join(dir, "commands.log") |
| 40 | + |
| 41 | + script := `#!/bin/sh |
| 42 | +echo "$1" >> ` + logFile + "\n" |
| 43 | + |
| 44 | + for _, name := range []string{"mx", "mxbuild"} { |
| 45 | + path := filepath.Join(dir, name) |
| 46 | + if err := os.WriteFile(path, []byte(script), 0755); err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + } |
| 50 | + return dir, logFile |
| 51 | +} |
| 52 | + |
| 53 | +func TestCheck_UpdateWidgetsBeforeCheck(t *testing.T) { |
| 54 | + if runtime.GOOS == "windows" { |
| 55 | + t.Skip("shell script test not supported on Windows") |
| 56 | + } |
| 57 | + |
| 58 | + mxDir, logFile := createFakeMxDir(t) |
| 59 | + |
| 60 | + var stdout, stderr bytes.Buffer |
| 61 | + opts := CheckOptions{ |
| 62 | + ProjectPath: "/tmp/fake.mpr", |
| 63 | + MxBuildPath: mxDir, |
| 64 | + Stdout: &stdout, |
| 65 | + Stderr: &stderr, |
| 66 | + } |
| 67 | + |
| 68 | + Check(opts) |
| 69 | + |
| 70 | + logBytes, err := os.ReadFile(logFile) |
| 71 | + if err != nil { |
| 72 | + t.Fatalf("failed to read command log: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + log := string(logBytes) |
| 76 | + if !bytes.Contains(logBytes, []byte("update-widgets\n")) { |
| 77 | + t.Errorf("update-widgets was not called, got log:\n%s", log) |
| 78 | + } |
| 79 | + if !bytes.Contains(logBytes, []byte("check\n")) { |
| 80 | + t.Errorf("check was not called, got log:\n%s", log) |
| 81 | + } |
| 82 | + |
| 83 | + // Verify order: update-widgets before check |
| 84 | + uwIdx := bytes.Index(logBytes, []byte("update-widgets")) |
| 85 | + chIdx := bytes.Index(logBytes, []byte("check")) |
| 86 | + if uwIdx >= chIdx { |
| 87 | + t.Errorf("update-widgets should run before check, got log:\n%s", log) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestCheck_SkipUpdateWidgetsFlag(t *testing.T) { |
| 92 | + if runtime.GOOS == "windows" { |
| 93 | + t.Skip("shell script test not supported on Windows") |
| 94 | + } |
| 95 | + |
| 96 | + mxDir, logFile := createFakeMxDir(t) |
| 97 | + |
| 98 | + var stdout, stderr bytes.Buffer |
| 99 | + opts := CheckOptions{ |
| 100 | + ProjectPath: "/tmp/fake.mpr", |
| 101 | + MxBuildPath: mxDir, |
| 102 | + SkipUpdateWidgets: true, |
| 103 | + Stdout: &stdout, |
| 104 | + Stderr: &stderr, |
| 105 | + } |
| 106 | + |
| 107 | + Check(opts) |
| 108 | + |
| 109 | + logBytes, err := os.ReadFile(logFile) |
| 110 | + if err != nil { |
| 111 | + t.Fatalf("failed to read command log: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + if bytes.Contains(logBytes, []byte("update-widgets")) { |
| 115 | + t.Error("update-widgets should NOT be called when SkipUpdateWidgets=true") |
| 116 | + } |
| 117 | + if !bytes.Contains(logBytes, []byte("check")) { |
| 118 | + t.Error("check should still be called") |
| 119 | + } |
| 120 | +} |
0 commit comments