|
| 1 | +package build |
| 2 | + |
| 3 | +import ( |
| 4 | + "path/filepath" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + buildutils "github.com/jfrog/build-info-go/build/utils" |
| 10 | + "github.com/jfrog/build-info-go/tests" |
| 11 | + "github.com/jfrog/build-info-go/utils" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | +) |
| 14 | + |
| 15 | +var pnpmLogger = utils.NewDefaultLogger(utils.INFO) |
| 16 | + |
| 17 | +func TestGenerateBuildInfoForPnpm(t *testing.T) { |
| 18 | + pnpmVersion, execPath, err := buildutils.GetPnpmVersionAndExecPath(pnpmLogger) |
| 19 | + if err != nil { |
| 20 | + t.Skip("pnpm is not installed, skipping test") |
| 21 | + } |
| 22 | + |
| 23 | + service := NewBuildInfoService() |
| 24 | + pnpmBuild, err := service.GetOrCreateBuild("build-info-go-test-pnpm", strconv.FormatInt(time.Now().Unix(), 10)) |
| 25 | + assert.NoError(t, err) |
| 26 | + defer func() { |
| 27 | + assert.NoError(t, pnpmBuild.Clean()) |
| 28 | + }() |
| 29 | + |
| 30 | + // Create pnpm project. |
| 31 | + path, err := filepath.Abs(filepath.Join(".", "testdata")) |
| 32 | + assert.NoError(t, err) |
| 33 | + projectPath, cleanup := tests.CreatePnpmTest(t, path, "project1") |
| 34 | + defer cleanup() |
| 35 | + |
| 36 | + // Install dependencies in the pnpm project. |
| 37 | + _, _, err = buildutils.RunPnpmCmd(execPath, projectPath, []string{"install"}, pnpmLogger) |
| 38 | + assert.NoError(t, err) |
| 39 | + |
| 40 | + pnpmModule, err := pnpmBuild.AddPnpmModule(projectPath) |
| 41 | + assert.NoError(t, err) |
| 42 | + |
| 43 | + err = pnpmModule.CalcDependencies() |
| 44 | + assert.NoError(t, err) |
| 45 | + |
| 46 | + buildInfo, err := pnpmBuild.ToBuildInfo() |
| 47 | + assert.NoError(t, err) |
| 48 | + |
| 49 | + // Verify results - should have at least one module with dependencies |
| 50 | + assert.Len(t, buildInfo.Modules, 1) |
| 51 | + assert.Greater(t, len(buildInfo.Modules[0].Dependencies), 0, "Expected at least one dependency") |
| 52 | + |
| 53 | + // Verify pnpm version is supported |
| 54 | + assert.True(t, pnpmVersion.AtLeast("6.0.0"), "pnpm version should be at least 6.0.0") |
| 55 | +} |
| 56 | + |
| 57 | +func TestFilterPnpmArgsFlags(t *testing.T) { |
| 58 | + _, execPath, err := buildutils.GetPnpmVersionAndExecPath(pnpmLogger) |
| 59 | + if err != nil { |
| 60 | + t.Skip("pnpm is not installed, skipping test") |
| 61 | + } |
| 62 | + |
| 63 | + service := NewBuildInfoService() |
| 64 | + pnpmBuild, err := service.GetOrCreateBuild("build-info-go-test-pnpm", strconv.FormatInt(time.Now().Unix(), 10)) |
| 65 | + assert.NoError(t, err) |
| 66 | + defer func() { |
| 67 | + assert.NoError(t, pnpmBuild.Clean()) |
| 68 | + }() |
| 69 | + |
| 70 | + // Create pnpm project. |
| 71 | + path, err := filepath.Abs(filepath.Join(".", "testdata")) |
| 72 | + assert.NoError(t, err) |
| 73 | + projectPath, cleanup := tests.CreatePnpmTest(t, path, "project1") |
| 74 | + defer cleanup() |
| 75 | + |
| 76 | + // Install dependencies first |
| 77 | + _, _, err = buildutils.RunPnpmCmd(execPath, projectPath, []string{"install"}, pnpmLogger) |
| 78 | + assert.NoError(t, err) |
| 79 | + |
| 80 | + pnpmModule, err := pnpmBuild.AddPnpmModule(projectPath) |
| 81 | + assert.NoError(t, err) |
| 82 | + |
| 83 | + // Test filtering with command and flags |
| 84 | + pnpmArgs := []string{"ls", "--json", "--long"} |
| 85 | + pnpmModule.SetPnpmArgs(pnpmArgs) |
| 86 | + pnpmModule.filterPnpmArgsFlags() |
| 87 | + expected := []string{"--json", "--long"} |
| 88 | + assert.Equal(t, expected, pnpmModule.pnpmArgs) |
| 89 | + |
| 90 | + // Test filtering with only flags |
| 91 | + pnpmArgs = []string{"--prod", "--json"} |
| 92 | + pnpmModule.SetPnpmArgs(pnpmArgs) |
| 93 | + pnpmModule.filterPnpmArgsFlags() |
| 94 | + expected = []string{"--prod", "--json"} |
| 95 | + assert.Equal(t, expected, pnpmModule.pnpmArgs) |
| 96 | + |
| 97 | + // Test filtering with single command (no flags) |
| 98 | + pnpmArgs = []string{"install"} |
| 99 | + pnpmModule.SetPnpmArgs(pnpmArgs) |
| 100 | + pnpmModule.filterPnpmArgsFlags() |
| 101 | + expected = []string{} |
| 102 | + assert.Equal(t, expected, pnpmModule.pnpmArgs) |
| 103 | +} |
| 104 | + |
| 105 | +func TestPnpmModuleSetters(t *testing.T) { |
| 106 | + _, execPath, err := buildutils.GetPnpmVersionAndExecPath(pnpmLogger) |
| 107 | + if err != nil { |
| 108 | + t.Skip("pnpm is not installed, skipping test") |
| 109 | + } |
| 110 | + |
| 111 | + service := NewBuildInfoService() |
| 112 | + pnpmBuild, err := service.GetOrCreateBuild("build-info-go-test-pnpm", strconv.FormatInt(time.Now().Unix(), 10)) |
| 113 | + assert.NoError(t, err) |
| 114 | + defer func() { |
| 115 | + assert.NoError(t, pnpmBuild.Clean()) |
| 116 | + }() |
| 117 | + |
| 118 | + // Create pnpm project. |
| 119 | + path, err := filepath.Abs(filepath.Join(".", "testdata")) |
| 120 | + assert.NoError(t, err) |
| 121 | + projectPath, cleanup := tests.CreatePnpmTest(t, path, "project1") |
| 122 | + defer cleanup() |
| 123 | + |
| 124 | + // Install dependencies first |
| 125 | + _, _, err = buildutils.RunPnpmCmd(execPath, projectPath, []string{"install"}, pnpmLogger) |
| 126 | + assert.NoError(t, err) |
| 127 | + |
| 128 | + pnpmModule, err := pnpmBuild.AddPnpmModule(projectPath) |
| 129 | + assert.NoError(t, err) |
| 130 | + |
| 131 | + // Test SetName |
| 132 | + pnpmModule.SetName("custom-module-name") |
| 133 | + assert.Equal(t, "custom-module-name", pnpmModule.name) |
| 134 | + |
| 135 | + // Test SetPnpmArgs |
| 136 | + args := []string{"--prod", "--frozen-lockfile"} |
| 137 | + pnpmModule.SetPnpmArgs(args) |
| 138 | + assert.Equal(t, args, pnpmModule.pnpmArgs) |
| 139 | + |
| 140 | + // Test SetCollectBuildInfo |
| 141 | + pnpmModule.SetCollectBuildInfo(true) |
| 142 | + assert.True(t, pnpmModule.collectBuildInfo) |
| 143 | + pnpmModule.SetCollectBuildInfo(false) |
| 144 | + assert.False(t, pnpmModule.collectBuildInfo) |
| 145 | +} |
| 146 | + |
| 147 | +func TestPnpmModuleBuild(t *testing.T) { |
| 148 | + _, _, err := buildutils.GetPnpmVersionAndExecPath(pnpmLogger) |
| 149 | + if err != nil { |
| 150 | + t.Skip("pnpm is not installed, skipping test") |
| 151 | + } |
| 152 | + |
| 153 | + service := NewBuildInfoService() |
| 154 | + pnpmBuild, err := service.GetOrCreateBuild("build-info-go-test-pnpm", strconv.FormatInt(time.Now().Unix(), 10)) |
| 155 | + assert.NoError(t, err) |
| 156 | + defer func() { |
| 157 | + assert.NoError(t, pnpmBuild.Clean()) |
| 158 | + }() |
| 159 | + |
| 160 | + // Create pnpm project. |
| 161 | + path, err := filepath.Abs(filepath.Join(".", "testdata")) |
| 162 | + assert.NoError(t, err) |
| 163 | + projectPath, cleanup := tests.CreatePnpmTest(t, path, "project1") |
| 164 | + defer cleanup() |
| 165 | + |
| 166 | + pnpmModule, err := pnpmBuild.AddPnpmModule(projectPath) |
| 167 | + assert.NoError(t, err) |
| 168 | + |
| 169 | + // Test Build with install command |
| 170 | + pnpmModule.SetPnpmArgs([]string{"install"}) |
| 171 | + pnpmModule.SetCollectBuildInfo(false) |
| 172 | + err = pnpmModule.Build() |
| 173 | + assert.NoError(t, err) |
| 174 | + |
| 175 | + // Test Build with collectBuildInfo enabled |
| 176 | + pnpmModule.SetCollectBuildInfo(true) |
| 177 | + err = pnpmModule.Build() |
| 178 | + assert.NoError(t, err) |
| 179 | + |
| 180 | + buildInfo, err := pnpmBuild.ToBuildInfo() |
| 181 | + assert.NoError(t, err) |
| 182 | + assert.Len(t, buildInfo.Modules, 1) |
| 183 | +} |
| 184 | + |
| 185 | +func TestNewPnpmModule(t *testing.T) { |
| 186 | + _, _, err := buildutils.GetPnpmVersionAndExecPath(pnpmLogger) |
| 187 | + if err != nil { |
| 188 | + t.Skip("pnpm is not installed, skipping test") |
| 189 | + } |
| 190 | + |
| 191 | + service := NewBuildInfoService() |
| 192 | + pnpmBuild, err := service.GetOrCreateBuild("build-info-go-test-pnpm", strconv.FormatInt(time.Now().Unix(), 10)) |
| 193 | + assert.NoError(t, err) |
| 194 | + defer func() { |
| 195 | + assert.NoError(t, pnpmBuild.Clean()) |
| 196 | + }() |
| 197 | + |
| 198 | + // Create pnpm project. |
| 199 | + path, err := filepath.Abs(filepath.Join(".", "testdata")) |
| 200 | + assert.NoError(t, err) |
| 201 | + projectPath, cleanup := tests.CreatePnpmTest(t, path, "project1") |
| 202 | + defer cleanup() |
| 203 | + |
| 204 | + // Test creating module with explicit path |
| 205 | + pnpmModule, err := pnpmBuild.AddPnpmModule(projectPath) |
| 206 | + assert.NoError(t, err) |
| 207 | + assert.NotNil(t, pnpmModule) |
| 208 | + assert.Equal(t, projectPath, pnpmModule.srcPath) |
| 209 | + assert.NotEmpty(t, pnpmModule.executablePath) |
| 210 | +} |
0 commit comments