|
9 | 9 | "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" |
10 | 10 | "github.com/jfrog/jfrog-cli-core/v2/utils/ioutils" |
11 | 11 | "github.com/jfrog/jfrog-client-go/auth" |
12 | | - clientTestUtils "github.com/jfrog/jfrog-client-go/utils/tests" |
13 | 12 | "github.com/stretchr/testify/assert" |
14 | 13 | "github.com/stretchr/testify/require" |
15 | 14 | "golang.org/x/exp/slices" |
@@ -170,24 +169,14 @@ func TestSetupCommand_Pip(t *testing.T) { |
170 | 169 | func testSetupCommandPip(t *testing.T, packageManager project.ProjectType, customConfig bool) { |
171 | 170 | var pipConfFilePath string |
172 | 171 | if customConfig { |
| 172 | + // For custom configuration file, set the PIP_CONFIG_FILE environment variable to point to the temporary pip.conf file. |
173 | 173 | pipConfFilePath = filepath.Join(t.TempDir(), "pip.conf") |
174 | | - restoreEnv := clientTestUtils.SetEnvWithCallbackAndAssert(t, "PIP_CONFIG_FILE", pipConfFilePath) |
175 | | - defer restoreEnv() |
| 174 | + t.Setenv("PIP_CONFIG_FILE", pipConfFilePath) |
176 | 175 | } else { |
177 | | - if coreutils.IsWindows() { |
178 | | - pipConfFilePath = filepath.Join(os.Getenv("APPDATA"), "pip", "pip.ini") |
179 | | - } else { |
180 | | - // Retrieve the home directory and construct the pip.conf file path. |
181 | | - homeDir, err := os.UserHomeDir() |
182 | | - assert.NoError(t, err) |
183 | | - pipConfFilePath = filepath.Join(homeDir, ".config", "pip", "pip.conf") |
184 | | - } |
185 | | - // Back up the existing .pip.conf file and ensure restoration after the test. |
186 | | - restorePipConfFunc, err := ioutils.BackupFile(pipConfFilePath, ".pipconf.backup") |
187 | | - assert.NoError(t, err) |
188 | | - defer func() { |
189 | | - assert.NoError(t, restorePipConfFunc()) |
190 | | - }() |
| 176 | + // For global configuration file, back up the existing pip.conf file and ensure restoration after the test. |
| 177 | + var restoreFunc func() |
| 178 | + pipConfFilePath, restoreFunc = globalGlobalPipConfigPath(t) |
| 179 | + defer restoreFunc() |
191 | 180 | } |
192 | 181 |
|
193 | 182 | pipLoginCmd := createTestSetupCommand(packageManager) |
@@ -225,12 +214,30 @@ func testSetupCommandPip(t *testing.T, packageManager project.ProjectType, custo |
225 | 214 | } |
226 | 215 | } |
227 | 216 |
|
| 217 | +// globalGlobalPipConfigPath returns the path to the global pip.conf file and a backup function to restore the original file. |
| 218 | +func globalGlobalPipConfigPath(t *testing.T) (string, func()) { |
| 219 | + var pipConfFilePath string |
| 220 | + if coreutils.IsWindows() { |
| 221 | + pipConfFilePath = filepath.Join(os.Getenv("APPDATA"), "pip", "pip.ini") |
| 222 | + } else { |
| 223 | + // Retrieve the home directory and construct the pip.conf file path. |
| 224 | + homeDir, err := os.UserHomeDir() |
| 225 | + assert.NoError(t, err) |
| 226 | + pipConfFilePath = filepath.Join(homeDir, ".config", "pip", "pip.conf") |
| 227 | + } |
| 228 | + // Back up the existing .pip.conf file and ensure restoration after the test. |
| 229 | + restorePipConfFunc, err := ioutils.BackupFile(pipConfFilePath, ".pipconf.backup") |
| 230 | + assert.NoError(t, err) |
| 231 | + return pipConfFilePath, func() { |
| 232 | + assert.NoError(t, restorePipConfFunc()) |
| 233 | + } |
| 234 | +} |
| 235 | + |
228 | 236 | func TestSetupCommand_configurePoetry(t *testing.T) { |
229 | 237 | configDir := t.TempDir() |
230 | 238 | poetryConfigFilePath := filepath.Join(configDir, "config.toml") |
231 | 239 | poetryAuthFilePath := filepath.Join(configDir, "auth.toml") |
232 | | - restoreEnv := clientTestUtils.SetEnvWithCallbackAndAssert(t, "POETRY_CONFIG_DIR", configDir) |
233 | | - defer restoreEnv() |
| 240 | + t.Setenv("POETRY_CONFIG_DIR", configDir) |
234 | 241 | poetryLoginCmd := createTestSetupCommand(project.Poetry) |
235 | 242 |
|
236 | 243 | for _, testCase := range testCases { |
@@ -279,8 +286,7 @@ func TestSetupCommand_configurePoetry(t *testing.T) { |
279 | 286 | func TestSetupCommand_Go(t *testing.T) { |
280 | 287 | goProxyEnv := "GOPROXY" |
281 | 288 | // Restore the original value of the GOPROXY environment variable after the test. |
282 | | - restoreGoProxy := clientTestUtils.SetEnvWithCallbackAndAssert(t, goProxyEnv, "") |
283 | | - defer restoreGoProxy() |
| 289 | + t.Setenv(goProxyEnv, "") |
284 | 290 |
|
285 | 291 | // Assuming createTestSetupCommand initializes your Go login command |
286 | 292 | goLoginCmd := createTestSetupCommand(project.Go) |
@@ -329,12 +335,10 @@ func testBuildToolLoginCommandConfigureDotnetNuget(t *testing.T, packageManager |
329 | 335 | // Set the NuGet.config file path to a custom location. |
330 | 336 | if packageManager == project.Dotnet { |
331 | 337 | nugetConfigFilePath = filepath.Join(t.TempDir(), "NuGet.Config") |
332 | | - restoreEnv := clientTestUtils.SetEnvWithCallbackAndAssert(t, "DOTNET_CLI_HOME", filepath.Dir(nugetConfigFilePath)) |
333 | | - defer restoreEnv() |
| 338 | + t.Setenv("DOTNET_CLI_HOME", filepath.Dir(nugetConfigFilePath)) |
334 | 339 | } else { |
335 | 340 | nugetConfigFilePath = filepath.Join(t.TempDir(), "nuget.config") |
336 | | - restoreEnv := clientTestUtils.SetEnvWithCallbackAndAssert(t, "NUGET_CONFIG_FILE", nugetConfigFilePath) |
337 | | - defer restoreEnv() |
| 341 | + t.Setenv("NUGET_CONFIG_FILE", nugetConfigFilePath) |
338 | 342 | } |
339 | 343 |
|
340 | 344 | nugetLoginCmd := createTestSetupCommand(packageManager) |
|
0 commit comments