Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Go's t.TempDir() for tests #272

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions build/gradle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@ var downloadExtractorsFromReleasesCases = []struct {
func TestDownloadExtractorsFromReleases(t *testing.T) {
for _, testCase := range downloadExtractorsFromReleasesCases {
t.Run(testCase.extractorVersion, func(t *testing.T) {
tempDirPath, err := utils.CreateTempDir()
assert.NoError(t, err)
tempDirPath := t.TempDir()
defer func() {
assert.NoError(t, utils.RemoveTempDir(tempDirPath))
assert.NoError(t, utils.CleanOldDirs())
}()

// Download JAR
err = downloadGradleDependencies(tempDirPath, testCase.extractorVersion, nil, &utils.NullLog{})
err := downloadGradleDependencies(tempDirPath, testCase.extractorVersion, nil, &utils.NullLog{})
assert.NoError(t, err)

// Make sure the Gradle build-info extractor JAR exist
Expand Down
9 changes: 3 additions & 6 deletions build/maven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import (
)

func TestDownloadDependencies(t *testing.T) {
tempDirPath, err := utils.CreateTempDir()
assert.NoError(t, err)
tempDirPath := t.TempDir()
defer func() {
assert.NoError(t, utils.RemoveTempDir(tempDirPath))
assert.NoError(t, utils.CleanOldDirs())
}()

// Download JAR and create classworlds.conf
err = downloadMavenExtractor(tempDirPath, nil, &utils.NullLog{})
err := downloadMavenExtractor(tempDirPath, nil, &utils.NullLog{})
assert.NoError(t, err)

// Make sure the Maven build-info extractor JAR and the classwords.conf file exist.
Expand All @@ -47,8 +45,7 @@ func TestGenerateBuildInfoForMavenProject(t *testing.T) {
assert.NoError(t, err)
// Create maven project
projectPath := filepath.Join(testdataDir, "maven", "project")
tmpProjectPath, cleanup := tests.CreateTestProject(t, projectPath)
defer cleanup()
tmpProjectPath := tests.CreateTestProject(t, projectPath)
// Add maven project as module in build-info.
mavenModule, err := mavenBuild.AddMavenModule(tmpProjectPath)
assert.NoError(t, err)
Expand Down
6 changes: 2 additions & 4 deletions build/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func TestGenerateBuildInfoForNpm(t *testing.T) {
// Create npm project.
path, err := filepath.Abs(filepath.Join(".", "testdata"))
assert.NoError(t, err)
projectPath, cleanup := tests.CreateNpmTest(t, path, "project3", false, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project3", false, npmVersion)

// Install dependencies in the npm project.
npmArgs := []string{"--cache=" + filepath.Join(projectPath, "tmpcache")}
Expand Down Expand Up @@ -66,8 +65,7 @@ func TestFilterNpmArgsFlags(t *testing.T) {
// Create npm project.
path, err := filepath.Abs(filepath.Join(".", "testdata"))
assert.NoError(t, err)
projectPath, cleanup := tests.CreateNpmTest(t, path, "project3", false, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project3", false, npmVersion)

// Set arguments in npmArgs.
npmArgs := []string{"ls", "--package-lock-only"}
Expand Down
3 changes: 1 addition & 2 deletions build/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func testGenerateBuildInfoForPython(t *testing.T, pythonTool pythonutils.PythonT
assert.NoError(t, err)
// Create python project
projectPath := filepath.Join(testdataDir, "python", string(pythonTool))
tmpProjectPath, cleanup := tests.CreateTestProject(t, projectPath)
defer cleanup()
tmpProjectPath := tests.CreateTestProject(t, projectPath)

// Install dependencies in the pip project.
pythonModule, err := pythonBuild.AddPythonModule(tmpProjectPath, pythonTool)
Expand Down
28 changes: 9 additions & 19 deletions build/utils/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ func TestReadPackageInfoFromPackageJsonIfExists(t *testing.T) {
assert.NoError(t, err)
path, err := filepath.Abs(filepath.Join("..", "testdata"))
assert.NoError(t, err)
projectPath, cleanup := tests.CreateNpmTest(t, path, "project1", false, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project1", false, npmVersion)

// Prepare test cases
testCases := []struct {
Expand Down Expand Up @@ -83,9 +82,7 @@ func TestReadPackageInfoFromPackageJsonIfExistErr(t *testing.T) {
// Prepare test data
npmVersion, _, err := GetNpmVersionAndExecPath(logger)
assert.NoError(t, err)
tempDir, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t)
assert.NoError(t, err)
defer createTempDirCallback()
tempDir := t.TempDir()

// Create bad package.json file and expect error
assert.NoError(t, os.WriteFile(filepath.Join(tempDir, "package.json"), []byte("non json file"), 0600))
Expand Down Expand Up @@ -180,8 +177,7 @@ func TestBundledDependenciesList(t *testing.T) {
path, err := filepath.Abs(filepath.Join("..", "testdata"))
assert.NoError(t, err)

projectPath, cleanup := tests.CreateNpmTest(t, path, "project1", false, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project1", false, npmVersion)
cacachePath := filepath.Join(projectPath, "tmpcache")
npmArgs := []string{"--cache=" + cacachePath}

Expand All @@ -200,8 +196,7 @@ func TestConflictsDependenciesList(t *testing.T) {
path, err := filepath.Abs(filepath.Join("..", "testdata"))
assert.NoError(t, err)

projectPath, cleanup := tests.CreateNpmTest(t, path, "project5", true, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project5", true, npmVersion)
cacachePath := filepath.Join(projectPath, "tmpcache")
npmArgs := []string{"--cache=" + cacachePath}

Expand All @@ -218,8 +213,7 @@ func TestDependencyWithNoIntegrity(t *testing.T) {
// Create the second npm project which has a transitive dependency without integrity (ansi-regex:5.0.0).
path, err := filepath.Abs(filepath.Join("..", "testdata"))
assert.NoError(t, err)
projectPath, cleanup := tests.CreateNpmTest(t, path, "project2", true, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project2", true, npmVersion)

// Run npm CI to create this special case where the 'ansi-regex:5.0.0' is missing the integrity.
npmArgs := []string{"--cache=" + filepath.Join(projectPath, "tmpcache")}
Expand All @@ -241,8 +235,7 @@ func TestDependencyPackageLockOnly(t *testing.T) {
if !npmVersion.AtLeast("7.0.0") {
t.Skip("Running on npm v7 and above only, skipping...")
}
path, cleanup := tests.CreateTestProject(t, filepath.Join("..", "testdata/npm/project6"))
defer cleanup()
path := tests.CreateTestProject(t, filepath.Join("..", "testdata/npm/project6"))
assert.NoError(t, utils.MoveFile(filepath.Join(path, "package-lock_test.json"), filepath.Join(path, "package-lock.json")))
// sleep so the package.json modified time will be bigger than the package-lock.json, this make sure it will recalculate lock file.
require.NoError(t, os.Chtimes(filepath.Join(path, "package.json"), time.Now(), time.Now().Add(time.Millisecond*20)))
Expand Down Expand Up @@ -318,8 +311,7 @@ func TestDependenciesTreeDifferentBetweenOKs(t *testing.T) {
assert.NoError(t, err)
path, err := filepath.Abs(filepath.Join("..", "testdata"))
assert.NoError(t, err)
projectPath, cleanup := tests.CreateNpmTest(t, path, "project4", true, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project4", true, npmVersion)
cacachePath := filepath.Join(projectPath, "tmpcache")

// Install all the project's dependencies.
Expand Down Expand Up @@ -357,8 +349,7 @@ func TestNpmProdFlag(t *testing.T) {
}
for _, entry := range testDependencyScopes {
func() {
projectPath, cleanup := tests.CreateNpmTest(t, path, "project3", false, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project3", false, npmVersion)
cacachePath := filepath.Join(projectPath, "tmpcache")
npmArgs := []string{"--cache=" + cacachePath, entry.scope}

Expand All @@ -382,8 +373,7 @@ func TestGetConfigCacheNpmIntegration(t *testing.T) {
// Create the first npm project which contains peerDependencies, devDependencies & bundledDependencies
path, err := filepath.Abs(filepath.Join("..", "testdata"))
assert.NoError(t, err)
projectPath, cleanup := tests.CreateNpmTest(t, path, "project1", false, npmVersion)
defer cleanup()
projectPath := tests.CreateNpmTest(t, path, "project1", false, npmVersion)
cachePath := filepath.Join(projectPath, "tmpcache")
npmArgs := []string{"--cache=" + cachePath}

Expand Down
6 changes: 1 addition & 5 deletions build/utils/yarn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ func TestBuildYarnV1Dependencies(t *testing.T) {

func checkGetYarnDependencies(t *testing.T, versionDir string, expectedLocators []string) {
// Copy the project directory to a temporary directory
tempDirPath, err := utils.CreateTempDir()
assert.NoError(t, err, "Couldn't create temp dir")
defer func() {
assert.NoError(t, utils.RemoveTempDir(tempDirPath), "Couldn't remove temp dir")
}()
tempDirPath := t.TempDir()
testDataSource := filepath.Join("..", "testdata", "yarn", versionDir)
testDataTarget := filepath.Join(tempDirPath, "yarn")
assert.NoError(t, utils.CopyDir(testDataSource, testDataTarget, true, nil))
Expand Down
14 changes: 4 additions & 10 deletions build/yarn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"testing"

buildutils "github.com/jfrog/build-info-go/build/utils"
"github.com/jfrog/build-info-go/tests"

"github.com/jfrog/build-info-go/entities"
"github.com/jfrog/build-info-go/utils"
"github.com/jfrog/gofrog/parallel"
Expand Down Expand Up @@ -81,8 +79,7 @@ func TestAppendDependencyRecursively(t *testing.T) {

func TestGenerateBuildInfoForYarnProject(t *testing.T) {
// Copy the project directory to a temporary directory
tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
tempDirPath := t.TempDir()
testDataSource := filepath.Join("testdata", "yarn", "v2")
testDataTarget := filepath.Join(tempDirPath, "yarn")
assert.NoError(t, utils.CopyDir(testDataSource, testDataTarget, true, nil))
Expand All @@ -107,8 +104,7 @@ func TestGenerateBuildInfoForYarnProject(t *testing.T) {

func TestCollectDepsForYarnProjectWithTraverse(t *testing.T) {
// Copy the project directory to a temporary directory
tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
tempDirPath := t.TempDir()
testDataSource := filepath.Join("testdata", "yarn", "v2")
testDataTarget := filepath.Join(tempDirPath, "yarn")
assert.NoError(t, utils.CopyDir(testDataSource, testDataTarget, true, nil))
Expand Down Expand Up @@ -148,8 +144,7 @@ func TestCollectDepsForYarnProjectWithTraverse(t *testing.T) {

func TestCollectDepsForYarnProjectWithErrorInTraverse(t *testing.T) {
// Copy the project directory to a temporary directory
tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
tempDirPath := t.TempDir()
testDataSource := filepath.Join("testdata", "yarn", "v2")
testDataTarget := filepath.Join(tempDirPath, "yarn")
assert.NoError(t, utils.CopyDir(testDataSource, testDataTarget, true, nil))
Expand All @@ -171,8 +166,7 @@ func TestCollectDepsForYarnProjectWithErrorInTraverse(t *testing.T) {

func TestBuildYarnProjectWithArgs(t *testing.T) {
// Copy the project directory to a temporary directory
tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
tempDirPath := t.TempDir()
testDataSource := filepath.Join("testdata", "yarn", "v2")
testDataTarget := filepath.Join(tempDirPath, "yarn")
assert.NoError(t, utils.CopyDir(testDataSource, testDataTarget, true, nil))
Expand Down
3 changes: 1 addition & 2 deletions buildinfoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ func validateBuildInfoSchema(t *testing.T, commandName, pathInTestData string, i
func prepareProject(t *testing.T, pathInTestdata string, install func()) func() {
wd, err := os.Getwd()
assert.NoError(t, err)
tempDir, cleanup := tests.CreateTestProject(t, filepath.Join("build", "testdata", pathInTestdata))
tempDir := tests.CreateTestProject(t, filepath.Join("build", "testdata", pathInTestdata))
assert.NoError(t, os.Chdir(tempDir))
install()

return func() {
assert.NoError(t, os.Chdir(wd))
cleanup()
}
}

Expand Down
19 changes: 3 additions & 16 deletions tests/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ import (
// Copy a project from path to temp dir.
// projectPath - Local path to a project
// Return the copied project location and a cleanup function to delete it.
func CreateTestProject(t *testing.T, projectPath string) (tmpProjectPath string, cleanup func()) {
var err error
tmpProjectPath, err = utils.CreateTempDir()
assert.NoError(t, err)
func CreateTestProject(t *testing.T, projectPath string) (tmpProjectPath string) {
tmpProjectPath = t.TempDir()
assert.NoError(t, utils.CopyDir(projectPath, tmpProjectPath, true, nil))
cleanup = func() {
assert.NoError(t, utils.RemoveTempDir(tmpProjectPath))
}
return
}

Expand All @@ -39,7 +34,7 @@ func GetBuildInfo(t *testing.T, filePath string) entities.BuildInfo {
// withOsInPath - some tests have individual cases for specific os, if true, return the tests for that belong to the current running os.
// testdataPath - abs path to testdata dir.
// projectDirName - name of the project's directory.
func CreateNpmTest(t *testing.T, testdataPath, projectDirName string, withOsInPath bool, version *version.Version) (tmpProjectPath string, cleanup func()) {
func CreateNpmTest(t *testing.T, testdataPath, projectDirName string, withOsInPath bool, version *version.Version) (tmpProjectPath string) {
var npmVersionDir string
switch {
case version.AtLeast("8.0.0"):
Expand Down Expand Up @@ -71,11 +66,3 @@ func PrintBuildInfoMismatch(t *testing.T, expected, actual []entities.Module) {
assert.NoError(t, err)
t.Errorf("build-info don't match. want: \n%v\ngot:\n%s\n", string(excpectedStr), string(actualStr))
}

func CreateTempDirWithCallbackAndAssert(t *testing.T) (string, func()) {
tempDirPath, err := utils.CreateTempDir()
assert.NoError(t, err, "Couldn't create temp dir")
return tempDirPath, func() {
assert.NoError(t, utils.RemoveTempDir(tempDirPath), "Couldn't remove temp dir")
}
}
3 changes: 1 addition & 2 deletions utils/pythonutils/piputils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ var moduleNameTestProvider = []struct {
func TestDetermineModuleName(t *testing.T) {
for _, test := range moduleNameTestProvider {
t.Run(strings.Join([]string{test.projectName, test.moduleName}, "/"), func(t *testing.T) {
tmpProjectPath, cleanup := tests.CreateTestProject(t, filepath.Join("..", "testdata", "pip", test.projectName))
defer cleanup()
tmpProjectPath := tests.CreateTestProject(t, filepath.Join("..", "testdata", "pip", test.projectName))

// Determine module name
packageName, err := getPackageNameFromSetuppy(tmpProjectPath)
Expand Down
6 changes: 2 additions & 4 deletions utils/pythonutils/poetryutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func TestGetProjectNameFromPyproject(t *testing.T) {

for _, testCase := range testCases {
t.Run(testCase.poetryProject, func(t *testing.T) {
tmpProjectPath, cleanup := tests.CreateTestProject(t, filepath.Join("..", "testdata", "poetry", testCase.poetryProject))
defer cleanup()
tmpProjectPath := tests.CreateTestProject(t, filepath.Join("..", "testdata", "poetry", testCase.poetryProject))

actualValue, err := extractProjectFromPyproject(filepath.Join(tmpProjectPath, "pyproject.toml"))
assert.NoError(t, err)
Expand All @@ -45,8 +44,7 @@ func TestGetProjectDependencies(t *testing.T) {

for _, testCase := range testCases {
t.Run(testCase.poetryProject, func(t *testing.T) {
tmpProjectPath, cleanup := tests.CreateTestProject(t, filepath.Join("..", "testdata", "poetry", testCase.poetryProject))
defer cleanup()
tmpProjectPath := tests.CreateTestProject(t, filepath.Join("..", "testdata", "poetry", testCase.poetryProject))

graph, directDependencies, err := getPoetryDependencies(tmpProjectPath)
assert.NoError(t, err)
Expand Down
Loading