code-intelligence.com/cifuzz@v0.40.0/internal/cmdutils/resolve/resolve_test.go (about) 1 package resolve 2 3 import ( 4 "os" 5 "path/filepath" 6 "runtime" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "code-intelligence.com/cifuzz/internal/config" 13 ) 14 15 func TestResolve(t *testing.T) { 16 originalWd, err := os.Getwd() 17 require.NoError(t, err) 18 revertToOriginalWd := func() { 19 err = os.Chdir(originalWd) 20 require.NoError(t, err) 21 } 22 23 changeWdToTestData := func(dir string) string { 24 err := os.Chdir(filepath.Join("testdata", dir)) 25 require.NoError(t, err) 26 pwd, err := os.Getwd() 27 require.NoError(t, err) 28 return pwd 29 } 30 31 t.Run("resolveBazel", func(t *testing.T) { 32 defer revertToOriginalWd() 33 pwd := changeWdToTestData("bazel") 34 testResolveBazel(t, pwd) 35 }) 36 37 t.Run("resolveCmake", func(t *testing.T) { 38 defer revertToOriginalWd() 39 pwd := changeWdToTestData("cmake") 40 testResolveCMake(t, pwd) 41 }) 42 43 t.Run("testResolveMavenGradle", func(t *testing.T) { 44 defer revertToOriginalWd() 45 pwd := changeWdToTestData("maven_gradle") 46 testResolveMavenGradle(t, pwd) 47 }) 48 49 t.Run("testResolveMavenGradleWindowsPaths", func(t *testing.T) { 50 defer revertToOriginalWd() 51 pwd := changeWdToTestData("maven_gradle") 52 testResolveMavenGradleWindowsPaths(t, pwd) 53 }) 54 } 55 56 func testResolveBazel(t *testing.T, pwd string) { 57 fuzzTestName := "//src/fuzz_test_1:fuzz_test_1" 58 59 // relative path 60 srcFile := filepath.Join("src", "fuzz_test_1", "fuzz_test.cpp") 61 resolved, err := resolve(srcFile, config.BuildSystemBazel, pwd) 62 require.NoError(t, err) 63 require.Equal(t, fuzzTestName, resolved) 64 65 // absolute path 66 srcFile = filepath.Join(pwd, srcFile) 67 resolved, err = resolve(srcFile, config.BuildSystemBazel, pwd) 68 require.NoError(t, err) 69 require.Equal(t, fuzzTestName, resolved) 70 } 71 72 func testResolveCMake(t *testing.T, pwd string) { 73 fuzzTestName := "fuzz_test_1" 74 75 // relative path 76 srcFile := filepath.Join("src", "fuzz_test_1", "fuzz_test.cpp") 77 resolved, err := resolve(srcFile, config.BuildSystemCMake, pwd) 78 assert.NoError(t, err) 79 assert.Equal(t, fuzzTestName, resolved) 80 81 // absolute path 82 srcFile = filepath.Join(pwd, srcFile) 83 resolved, err = resolve(srcFile, config.BuildSystemCMake, pwd) 84 require.NoError(t, err) 85 require.Equal(t, fuzzTestName, resolved) 86 } 87 88 func testResolveMavenGradle(t *testing.T, pwd string) { 89 fuzzTestName := "com.example.fuzz_test_1.FuzzTestCase" 90 91 // Java file 92 // relative path 93 srcFile := filepath.Join("src", "test", "java", "com", "example", "fuzz_test_1", "FuzzTestCase.java") 94 resolved, err := resolve(srcFile, config.BuildSystemGradle, pwd) 95 assert.NoError(t, err) 96 assert.Equal(t, fuzzTestName, resolved) 97 resolved, err = resolve(srcFile, config.BuildSystemMaven, pwd) 98 assert.NoError(t, err) 99 assert.Equal(t, fuzzTestName, resolved) 100 101 // absolute path 102 srcFile = filepath.Join(pwd, srcFile) 103 resolved, err = resolve(srcFile, config.BuildSystemGradle, pwd) 104 assert.NoError(t, err) 105 assert.Equal(t, fuzzTestName, resolved) 106 resolved, err = resolve(srcFile, config.BuildSystemMaven, pwd) 107 assert.NoError(t, err) 108 assert.Equal(t, fuzzTestName, resolved) 109 110 // Kotlin file 111 // relative path 112 srcFile = filepath.Join("src", "test", "kotlin", "com", "example", "fuzz_test_1", "FuzzTestCase.kt") 113 resolved, err = resolve(srcFile, config.BuildSystemGradle, pwd) 114 assert.NoError(t, err) 115 assert.Equal(t, fuzzTestName, resolved) 116 resolved, err = resolve(srcFile, config.BuildSystemMaven, pwd) 117 assert.NoError(t, err) 118 assert.Equal(t, fuzzTestName, resolved) 119 120 // absolute path 121 srcFile = filepath.Join(pwd, srcFile) 122 resolved, err = resolve(srcFile, config.BuildSystemGradle, pwd) 123 assert.NoError(t, err) 124 assert.Equal(t, fuzzTestName, resolved) 125 resolved, err = resolve(srcFile, config.BuildSystemMaven, pwd) 126 assert.NoError(t, err) 127 assert.Equal(t, fuzzTestName, resolved) 128 } 129 130 func testResolveMavenGradleWindowsPaths(t *testing.T, pwd string) { 131 if runtime.GOOS != "windows" { 132 t.Skip() 133 } 134 135 fuzzTestName := "com.example.fuzz_test_1.FuzzTestCase" 136 137 srcFile := "src/test/java/com/example/fuzz_test_1/FuzzTestCase.java" 138 resolved, err := resolve(srcFile, config.BuildSystemGradle, pwd) 139 assert.NoError(t, err) 140 assert.Equal(t, fuzzTestName, resolved) 141 resolved, err = resolve(srcFile, config.BuildSystemMaven, pwd) 142 assert.NoError(t, err) 143 assert.Equal(t, fuzzTestName, resolved) 144 145 srcFile = "src\\test\\java\\com\\example\\fuzz_test_1\\FuzzTestCase.java" 146 resolved, err = resolve(srcFile, config.BuildSystemGradle, pwd) 147 assert.NoError(t, err) 148 assert.Equal(t, fuzzTestName, resolved) 149 resolved, err = resolve(srcFile, config.BuildSystemMaven, pwd) 150 assert.NoError(t, err) 151 assert.Equal(t, fuzzTestName, resolved) 152 }