diff --git a/build/maven_test.go b/build/maven_test.go index 0208b2a3..75d514f2 100644 --- a/build/maven_test.go +++ b/build/maven_test.go @@ -4,8 +4,8 @@ import ( "bytes" "encoding/json" "fmt" - testdatautils "github.com/jfrog/build-info-go/build/testdata" "github.com/jfrog/build-info-go/entities" + "github.com/jfrog/build-info-go/tests" "github.com/jfrog/build-info-go/utils" "os" "path/filepath" @@ -45,7 +45,7 @@ func TestGenerateBuildInfoForMavenProject(t *testing.T) { assert.NoError(t, err) // Create maven project projectPath := filepath.Join(testdataDir, "maven", "project") - tmpProjectPath, cleanup := testdatautils.CreateTestProject(t, projectPath) + tmpProjectPath, cleanup := tests.CreateTestProject(t, projectPath) defer cleanup() // Add maven project as module in build-info. mavenModule, err := mavenBuild.AddMavenModule(tmpProjectPath) @@ -65,7 +65,7 @@ func TestGenerateBuildInfoForMavenProject(t *testing.T) { match, err := entities.IsEqualModuleSlices(buildInfo.Modules, expectedModules) assert.NoError(t, err) if !match { - testdatautils.PrintBuildInfoMismatch(t, expectedModules, buildInfo.Modules) + tests.PrintBuildInfoMismatch(t, expectedModules, buildInfo.Modules) } } } diff --git a/build/npm.go b/build/npm.go index c26a9a4b..6fe77db7 100644 --- a/build/npm.go +++ b/build/npm.go @@ -43,7 +43,7 @@ func newNpmModule(srcPath string, containingBuild *Build) (*NpmModule, error) { } // Read module name - packageInfo, err := buildutils.ReadPackageInfoFromPackageJsonIfExist(srcPath, npmVersion) + packageInfo, err := buildutils.ReadPackageInfoFromPackageJsonIfExists(srcPath, npmVersion) if err != nil { return nil, err } diff --git a/build/npm_test.go b/build/npm_test.go index 47b9d6f2..4a04a59c 100644 --- a/build/npm_test.go +++ b/build/npm_test.go @@ -6,9 +6,9 @@ import ( "testing" "time" - testdatautils "github.com/jfrog/build-info-go/build/testdata" buildutils "github.com/jfrog/build-info-go/build/utils" "github.com/jfrog/build-info-go/entities" + "github.com/jfrog/build-info-go/tests" "github.com/jfrog/build-info-go/utils" "github.com/stretchr/testify/assert" ) @@ -28,7 +28,7 @@ func TestGenerateBuildInfoForNpm(t *testing.T) { // Create npm project. path, err := filepath.Abs(filepath.Join(".", "testdata")) assert.NoError(t, err) - projectPath, cleanup := testdatautils.CreateNpmTest(t, path, "project3", false, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project3", false, npmVersion) defer cleanup() // Install dependencies in the npm project. @@ -45,11 +45,11 @@ func TestGenerateBuildInfoForNpm(t *testing.T) { // Verify results. expectedBuildInfoJson := filepath.Join(projectPath, "expected_npm_buildinfo.json") - expectedBuildInfo := testdatautils.GetBuildInfo(t, expectedBuildInfoJson) + expectedBuildInfo := tests.GetBuildInfo(t, expectedBuildInfoJson) match, err := entities.IsEqualModuleSlices(buildInfo.Modules, expectedBuildInfo.Modules) assert.NoError(t, err) if !match { - testdatautils.PrintBuildInfoMismatch(t, expectedBuildInfo.Modules, buildInfo.Modules) + tests.PrintBuildInfoMismatch(t, expectedBuildInfo.Modules, buildInfo.Modules) } } @@ -66,7 +66,7 @@ func TestFilterNpmArgsFlags(t *testing.T) { // Create npm project. path, err := filepath.Abs(filepath.Join(".", "testdata")) assert.NoError(t, err) - projectPath, cleanup := testdatautils.CreateNpmTest(t, path, "project3", false, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project3", false, npmVersion) defer cleanup() // Set arguments in npmArgs. diff --git a/build/python_test.go b/build/python_test.go index ca3caa8e..c9e6c66e 100644 --- a/build/python_test.go +++ b/build/python_test.go @@ -9,7 +9,7 @@ import ( "github.com/jfrog/build-info-go/entities" "github.com/jfrog/build-info-go/utils/pythonutils" - testdatautils "github.com/jfrog/build-info-go/build/testdata" + "github.com/jfrog/build-info-go/tests" "github.com/stretchr/testify/assert" ) @@ -47,7 +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 := testdatautils.CreateTestProject(t, projectPath) + tmpProjectPath, cleanup := tests.CreateTestProject(t, projectPath) defer cleanup() // Install dependencies in the pip project. @@ -59,11 +59,11 @@ func testGenerateBuildInfoForPython(t *testing.T, pythonTool pythonutils.PythonT if assert.NoError(t, err) { // Verify results. expectedBuildInfoJson := filepath.Join(projectPath, expectedResultsJson) - expectedBuildInfo := testdatautils.GetBuildInfo(t, expectedBuildInfoJson) + expectedBuildInfo := tests.GetBuildInfo(t, expectedBuildInfoJson) match, err := entities.IsEqualModuleSlices(buildInfo.Modules, expectedBuildInfo.Modules) assert.NoError(t, err) if !match { - testdatautils.PrintBuildInfoMismatch(t, expectedBuildInfo.Modules, buildInfo.Modules) + tests.PrintBuildInfoMismatch(t, expectedBuildInfo.Modules, buildInfo.Modules) } } } diff --git a/build/utils/npm.go b/build/utils/npm.go index ffc95e77..d086cc11 100644 --- a/build/utils/npm.go +++ b/build/utils/npm.go @@ -443,8 +443,8 @@ type PackageInfo struct { } // Read and populate package name, version and scope from the package.json file in the provided directory. -// If package.json does not exist, return an empty PackageInfo object. -func ReadPackageInfoFromPackageJsonIfExist(packageJsonDirectory string, npmVersion *version.Version) (*PackageInfo, error) { +// If package.json does not exist, return an empty PackageInfo struct. +func ReadPackageInfoFromPackageJsonIfExists(packageJsonDirectory string, npmVersion *version.Version) (*PackageInfo, error) { packageJson, err := os.ReadFile(filepath.Join(packageJsonDirectory, "package.json")) if err != nil { if os.IsNotExist(err) { diff --git a/build/utils/npm_test.go b/build/utils/npm_test.go index 798541c8..f8363c6e 100644 --- a/build/utils/npm_test.go +++ b/build/utils/npm_test.go @@ -11,7 +11,7 @@ import ( "github.com/jfrog/build-info-go/entities" "github.com/stretchr/testify/require" - testdatautils "github.com/jfrog/build-info-go/build/testdata" + "github.com/jfrog/build-info-go/tests" "github.com/jfrog/build-info-go/utils" "github.com/stretchr/testify/assert" ) @@ -44,13 +44,13 @@ func TestReadPackageInfo(t *testing.T) { } } -func TestReadPackageInfoFromPackageJsonIfExist(t *testing.T) { +func TestReadPackageInfoFromPackageJsonIfExists(t *testing.T) { // Prepare tests data npmVersion, _, err := GetNpmVersionAndExecPath(logger) assert.NoError(t, err) path, err := filepath.Abs(filepath.Join("..", "testdata")) assert.NoError(t, err) - projectPath, cleanup := testdatautils.CreateNpmTest(t, path, "project1", false, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project1", false, npmVersion) defer cleanup() // Prepare test cases @@ -66,7 +66,7 @@ func TestReadPackageInfoFromPackageJsonIfExist(t *testing.T) { for _, testCase := range testCases { t.Run(testCase.testName, func(t *testing.T) { // Read package info - packageInfo, err := ReadPackageInfoFromPackageJsonIfExist(testCase.packageJsonDirectory, npmVersion) + packageInfo, err := ReadPackageInfoFromPackageJsonIfExists(testCase.packageJsonDirectory, npmVersion) assert.NoError(t, err) // Remove "v" prefix, if exist @@ -83,13 +83,13 @@ func TestReadPackageInfoFromPackageJsonIfExistErr(t *testing.T) { // Prepare test data npmVersion, _, err := GetNpmVersionAndExecPath(logger) assert.NoError(t, err) - tempDir, createTempDirCallback := testdatautils.CreateTempDirWithCallbackAndAssert(t) + tempDir, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) assert.NoError(t, err) defer createTempDirCallback() // Create bad package.json file and expect error assert.NoError(t, os.WriteFile(filepath.Join(tempDir, "package.json"), []byte("non json file"), 0600)) - _, err = ReadPackageInfoFromPackageJsonIfExist(tempDir, npmVersion) + _, err = ReadPackageInfoFromPackageJsonIfExists(tempDir, npmVersion) assert.IsType(t, &json.SyntaxError{}, err) } @@ -180,7 +180,7 @@ func TestBundledDependenciesList(t *testing.T) { path, err := filepath.Abs(filepath.Join("..", "testdata")) assert.NoError(t, err) - projectPath, cleanup := testdatautils.CreateNpmTest(t, path, "project1", false, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project1", false, npmVersion) defer cleanup() cacachePath := filepath.Join(projectPath, "tmpcache") npmArgs := []string{"--cache=" + cacachePath} @@ -200,7 +200,7 @@ func TestConflictsDependenciesList(t *testing.T) { path, err := filepath.Abs(filepath.Join("..", "testdata")) assert.NoError(t, err) - projectPath, cleanup := testdatautils.CreateNpmTest(t, path, "project5", true, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project5", true, npmVersion) defer cleanup() cacachePath := filepath.Join(projectPath, "tmpcache") npmArgs := []string{"--cache=" + cacachePath} @@ -218,7 +218,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 := testdatautils.CreateNpmTest(t, path, "project2", true, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project2", true, npmVersion) defer cleanup() // Run npm CI to create this special case where the 'ansi-regex:5.0.0' is missing the integrity. @@ -241,7 +241,7 @@ func TestDependencyPackageLockOnly(t *testing.T) { if !npmVersion.AtLeast("7.0.0") { t.Skip("Running on npm v7 and above only, skipping...") } - path, cleanup := testdatautils.CreateTestProject(t, filepath.Join("..", "testdata/npm/project6")) + path, cleanup := tests.CreateTestProject(t, filepath.Join("..", "testdata/npm/project6")) defer cleanup() 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. @@ -318,7 +318,7 @@ func TestDependenciesTreeDifferentBetweenOKs(t *testing.T) { assert.NoError(t, err) path, err := filepath.Abs(filepath.Join("..", "testdata")) assert.NoError(t, err) - projectPath, cleanup := testdatautils.CreateNpmTest(t, path, "project4", true, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project4", true, npmVersion) defer cleanup() cacachePath := filepath.Join(projectPath, "tmpcache") @@ -357,7 +357,7 @@ func TestNpmProdFlag(t *testing.T) { } for _, entry := range testDependencyScopes { func() { - projectPath, cleanup := testdatautils.CreateNpmTest(t, path, "project3", false, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project3", false, npmVersion) defer cleanup() cacachePath := filepath.Join(projectPath, "tmpcache") npmArgs := []string{"--cache=" + cacachePath, entry.scope} @@ -382,7 +382,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 := testdatautils.CreateNpmTest(t, path, "project1", false, npmVersion) + projectPath, cleanup := tests.CreateNpmTest(t, path, "project1", false, npmVersion) defer cleanup() cachePath := filepath.Join(projectPath, "tmpcache") npmArgs := []string{"--cache=" + cachePath} diff --git a/build/yarn.go b/build/yarn.go index 52f3e21a..fcd436ac 100644 --- a/build/yarn.go +++ b/build/yarn.go @@ -50,7 +50,7 @@ func newYarnModule(srcPath string, containingBuild *Build) (*YarnModule, error) } // Read module name - packageInfo, err := buildutils.ReadPackageInfoFromPackageJsonIfExist(srcPath, nil) + packageInfo, err := buildutils.ReadPackageInfoFromPackageJsonIfExists(srcPath, nil) if err != nil { return nil, err } diff --git a/build/yarn_test.go b/build/yarn_test.go index f5cd0228..7dc7b4e8 100644 --- a/build/yarn_test.go +++ b/build/yarn_test.go @@ -6,8 +6,8 @@ import ( "reflect" "testing" - testdatautils "github.com/jfrog/build-info-go/build/testdata" 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" @@ -81,7 +81,7 @@ func TestAppendDependencyRecursively(t *testing.T) { func TestGenerateBuildInfoForYarnProject(t *testing.T) { // Copy the project directory to a temporary directory - tempDirPath, createTempDirCallback := testdatautils.CreateTempDirWithCallbackAndAssert(t) + tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() testDataSource := filepath.Join("testdata", "yarn", "v2") testDataTarget := filepath.Join(tempDirPath, "yarn") @@ -107,7 +107,7 @@ func TestGenerateBuildInfoForYarnProject(t *testing.T) { func TestCollectDepsForYarnProjectWithTraverse(t *testing.T) { // Copy the project directory to a temporary directory - tempDirPath, createTempDirCallback := testdatautils.CreateTempDirWithCallbackAndAssert(t) + tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() testDataSource := filepath.Join("testdata", "yarn", "v2") testDataTarget := filepath.Join(tempDirPath, "yarn") @@ -148,7 +148,7 @@ func TestCollectDepsForYarnProjectWithTraverse(t *testing.T) { func TestCollectDepsForYarnProjectWithErrorInTraverse(t *testing.T) { // Copy the project directory to a temporary directory - tempDirPath, createTempDirCallback := testdatautils.CreateTempDirWithCallbackAndAssert(t) + tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() testDataSource := filepath.Join("testdata", "yarn", "v2") testDataTarget := filepath.Join(tempDirPath, "yarn") @@ -171,7 +171,7 @@ func TestCollectDepsForYarnProjectWithErrorInTraverse(t *testing.T) { func TestBuildYarnProjectWithArgs(t *testing.T) { // Copy the project directory to a temporary directory - tempDirPath, createTempDirCallback := testdatautils.CreateTempDirWithCallbackAndAssert(t) + tempDirPath, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) defer createTempDirCallback() testDataSource := filepath.Join("testdata", "yarn", "v2") testDataTarget := filepath.Join(tempDirPath, "yarn") diff --git a/buildinfoschema_test.go b/buildinfoschema_test.go index 1a069fc6..165a49a4 100644 --- a/buildinfoschema_test.go +++ b/buildinfoschema_test.go @@ -5,7 +5,7 @@ import ( "path/filepath" "testing" - "github.com/jfrog/build-info-go/build/testdata" + "github.com/jfrog/build-info-go/tests" buildutils "github.com/jfrog/build-info-go/build/utils" "github.com/jfrog/build-info-go/utils" @@ -67,7 +67,7 @@ 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 := testdata.CreateTestProject(t, filepath.Join("build", "testdata", pathInTestdata)) + tempDir, cleanup := tests.CreateTestProject(t, filepath.Join("build", "testdata", pathInTestdata)) assert.NoError(t, os.Chdir(tempDir)) install() diff --git a/build/testdata/test_helpers.go b/tests/test_helpers.go similarity index 98% rename from build/testdata/test_helpers.go rename to tests/test_helpers.go index d1a34e1d..9e336cf9 100644 --- a/build/testdata/test_helpers.go +++ b/tests/test_helpers.go @@ -1,4 +1,4 @@ -package testdata +package tests import ( "encoding/json" @@ -58,7 +58,7 @@ func CreateNpmTest(t *testing.T, testdataPath, projectDirName string, withOsInPa npmVersionDir = filepath.Join(npmVersionDir, "linux") default: - //MacOs + // MacOs npmVersionDir = filepath.Join(npmVersionDir, "macos") } } @@ -80,4 +80,4 @@ func CreateTempDirWithCallbackAndAssert(t *testing.T) (string, func()) { return tempDirPath, func() { assert.NoError(t, utils.RemoveTempDir(tempDirPath), "Couldn't remove temp dir") } -} \ No newline at end of file +} diff --git a/utils/pythonutils/piputils_test.go b/utils/pythonutils/piputils_test.go index 5a01d66e..d2f0064c 100644 --- a/utils/pythonutils/piputils_test.go +++ b/utils/pythonutils/piputils_test.go @@ -5,7 +5,7 @@ import ( "strings" "testing" - testdatautils "github.com/jfrog/build-info-go/build/testdata" + "github.com/jfrog/build-info-go/tests" "github.com/stretchr/testify/assert" ) @@ -45,7 +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 := testdatautils.CreateTestProject(t, filepath.Join("..", "testdata", "pip", test.projectName)) + tmpProjectPath, cleanup := tests.CreateTestProject(t, filepath.Join("..", "testdata", "pip", test.projectName)) defer cleanup() // Determine module name diff --git a/utils/pythonutils/poetryutils_test.go b/utils/pythonutils/poetryutils_test.go index 263f2759..7fb44c3f 100644 --- a/utils/pythonutils/poetryutils_test.go +++ b/utils/pythonutils/poetryutils_test.go @@ -6,12 +6,12 @@ import ( "sort" "testing" - testdatautils "github.com/jfrog/build-info-go/build/testdata" + "github.com/jfrog/build-info-go/tests" "github.com/stretchr/testify/assert" ) func TestGetProjectNameFromPyproject(t *testing.T) { - tests := []struct { + testCases := []struct { poetryProject string expectedProjectName string }{ @@ -19,22 +19,22 @@ func TestGetProjectNameFromPyproject(t *testing.T) { {"nodevdeps", "my-poetry-project:1.1.17"}, } - for _, test := range tests { - t.Run(test.poetryProject, func(t *testing.T) { - tmpProjectPath, cleanup := testdatautils.CreateTestProject(t, filepath.Join("..", "testdata", "poetry", test.poetryProject)) + 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() actualValue, err := extractProjectFromPyproject(filepath.Join(tmpProjectPath, "pyproject.toml")) assert.NoError(t, err) - if actualValue.Name != test.expectedProjectName { - t.Errorf("Expected value: %s, got: %s.", test.expectedProjectName, actualValue) + if actualValue.Name != testCase.expectedProjectName { + t.Errorf("Expected value: %s, got: %s.", testCase.expectedProjectName, actualValue) } }) } } func TestGetProjectDependencies(t *testing.T) { - tests := []struct { + testCases := []struct { poetryProject string expectedDirectDependencies []string expectedTransitiveDependencies [][]string @@ -43,22 +43,22 @@ func TestGetProjectDependencies(t *testing.T) { {"nodevdeps", []string{"numpy:1.23.0", "python:"}, [][]string{nil, nil, nil}}, } - for _, test := range tests { - t.Run(test.poetryProject, func(t *testing.T) { - tmpProjectPath, cleanup := testdatautils.CreateTestProject(t, filepath.Join("..", "testdata", "poetry", test.poetryProject)) + 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() graph, directDependencies, err := getPoetryDependencies(tmpProjectPath) assert.NoError(t, err) sort.Strings(directDependencies) - if !reflect.DeepEqual(directDependencies, test.expectedDirectDependencies) { - t.Errorf("Expected value: %s, got: %s.", test.expectedDirectDependencies, directDependencies) + if !reflect.DeepEqual(directDependencies, testCase.expectedDirectDependencies) { + t.Errorf("Expected value: %s, got: %s.", testCase.expectedDirectDependencies, directDependencies) } for i, directDependency := range directDependencies { transitiveDependencies := graph[directDependency] sort.Strings(transitiveDependencies) - if !reflect.DeepEqual(transitiveDependencies, test.expectedTransitiveDependencies[i]) { - t.Errorf("Expected value: %s, got: %s.", test.expectedTransitiveDependencies[i], graph[directDependency]) + if !reflect.DeepEqual(transitiveDependencies, testCase.expectedTransitiveDependencies[i]) { + t.Errorf("Expected value: %s, got: %s.", testCase.expectedTransitiveDependencies[i], graph[directDependency]) } } })