|
| 1 | +package mvn |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestResolveMavenExecutable(t *testing.T) { |
| 14 | + wrapperName := "mvnw" |
| 15 | + if coreutils.IsWindows() { |
| 16 | + wrapperName = "mvnw.cmd" |
| 17 | + } |
| 18 | + |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + preferWrapper bool |
| 22 | + // setupCwd creates the fixture and returns the directory to chdir into, plus the |
| 23 | + // directory the wrapper was actually placed in ("" if no wrapper was created). |
| 24 | + setupCwd func(t *testing.T) (cwd string, wrapperRoot string) |
| 25 | + expectedExe string |
| 26 | + expectErr bool |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "jf mvn without wrapper present - falls back to PATH mvn", |
| 30 | + preferWrapper: false, |
| 31 | + setupCwd: func(t *testing.T) (string, string) { |
| 32 | + return t.TempDir(), "" |
| 33 | + }, |
| 34 | + expectedExe: "mvn", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "jf mvn with wrapper present - still uses PATH mvn (opt-in only via mvnw)", |
| 38 | + preferWrapper: false, |
| 39 | + setupCwd: func(t *testing.T) (string, string) { |
| 40 | + root := t.TempDir() |
| 41 | + createWrapperFixture(t, root, wrapperName) |
| 42 | + return root, root |
| 43 | + }, |
| 44 | + expectedExe: "mvn", |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "jf mvnw with wrapper in cwd - uses wrapper", |
| 48 | + preferWrapper: true, |
| 49 | + setupCwd: func(t *testing.T) (string, string) { |
| 50 | + root := t.TempDir() |
| 51 | + createWrapperFixture(t, root, wrapperName) |
| 52 | + return root, root |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "jf mvnw with wrapper only in parent dir - upward search finds it", |
| 57 | + preferWrapper: true, |
| 58 | + setupCwd: func(t *testing.T) (string, string) { |
| 59 | + root := t.TempDir() |
| 60 | + createWrapperFixture(t, root, wrapperName) |
| 61 | + subDir := filepath.Join(root, "module-a") |
| 62 | + require.NoError(t, os.MkdirAll(subDir, 0755)) |
| 63 | + return subDir, root |
| 64 | + }, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "jf mvnw without wrapper anywhere - errors, no fallback", |
| 68 | + preferWrapper: true, |
| 69 | + setupCwd: func(t *testing.T) (string, string) { |
| 70 | + return t.TempDir(), "" |
| 71 | + }, |
| 72 | + expectErr: true, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(tt.name, func(t *testing.T) { |
| 78 | + origWd, err := os.Getwd() |
| 79 | + require.NoError(t, err) |
| 80 | + defer func() { |
| 81 | + require.NoError(t, os.Chdir(origWd)) |
| 82 | + }() |
| 83 | + |
| 84 | + cwd, wrapperRoot := tt.setupCwd(t) |
| 85 | + require.NoError(t, os.Chdir(cwd)) |
| 86 | + |
| 87 | + exe, err := resolveMavenExecutable(tt.preferWrapper) |
| 88 | + if tt.expectErr { |
| 89 | + assert.Error(t, err) |
| 90 | + return |
| 91 | + } |
| 92 | + require.NoError(t, err) |
| 93 | + if tt.expectedExe != "" { |
| 94 | + assert.Equal(t, tt.expectedExe, exe) |
| 95 | + return |
| 96 | + } |
| 97 | + // Compare file identity rather than the path string: Windows CI runners can report |
| 98 | + // the same directory via a short (8.3) alias (e.g. "RUNNER~1" vs "runneradmin"), |
| 99 | + // which would make a plain string comparison fail even though exe correctly points |
| 100 | + // at the wrapper script. |
| 101 | + expectedInfo, err := os.Stat(filepath.Join(wrapperRoot, wrapperName)) |
| 102 | + require.NoError(t, err) |
| 103 | + actualInfo, err := os.Stat(exe) |
| 104 | + require.NoError(t, err) |
| 105 | + assert.True(t, os.SameFile(expectedInfo, actualInfo), "resolved executable %q does not refer to the expected wrapper in %q", exe, wrapperRoot) |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// createWrapperFixture creates a minimal Maven Wrapper marker (.mvn dir + wrapper script) at root. |
| 111 | +func createWrapperFixture(t *testing.T, root, wrapperName string) { |
| 112 | + require.NoError(t, os.MkdirAll(filepath.Join(root, ".mvn", "wrapper"), 0755)) |
| 113 | + require.NoError(t, os.WriteFile(filepath.Join(root, wrapperName), []byte("#!/bin/sh\n"), 0755)) |
| 114 | +} |
0 commit comments