@@ -6,11 +6,13 @@ import (
66 "io"
77 "net/http"
88 "net/http/httptest"
9+ "os"
910 "sort"
1011 "testing"
1112 "time"
1213
1314 "github.com/stretchr/testify/assert"
15+ "gopkg.in/yaml.v3"
1416)
1517
1618func TestGetRepositoryLanguages (t * testing.T ) {
@@ -220,3 +222,96 @@ func TestGetRepositoryLanguages(t *testing.T) {
220222 })
221223 }
222224}
225+
226+ func TestCreateLanguagesConfigFile_ExtensionsFromRepository (t * testing.T ) {
227+ tempDir := t .TempDir ()
228+
229+ // Mock API server for getRepositoryLanguages
230+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
231+ w .Header ().Set ("Content-Type" , "application/json" )
232+ json .NewEncoder (w ).Encode (map [string ]interface {}{
233+ "languages" : []map [string ]interface {}{
234+ {
235+ "name" : "JavaScript" ,
236+ "codacyDefaults" : []string {".js" , ".jsx" },
237+ "extensions" : []string {".js" , ".vue" },
238+ "enabled" : true ,
239+ "detected" : true ,
240+ },
241+ {
242+ "name" : "Python" ,
243+ "codacyDefaults" : []string {".py" },
244+ "extensions" : []string {".testPy" },
245+ "enabled" : true ,
246+ "detected" : true ,
247+ },
248+ {
249+ "name" : "Apex" ,
250+ "codacyDefaults" : []string {".cls" },
251+ "extensions" : []string {".app" , ".trigger" },
252+ "enabled" : true ,
253+ "detected" : true ,
254+ },
255+ {
256+ "name" : "Scala" ,
257+ "codacyDefaults" : []string {".scala" },
258+ "extensions" : []string {},
259+ "enabled" : true ,
260+ "detected" : true ,
261+ },
262+ {
263+ "name" : "Ruby" ,
264+ "codacyDefaults" : []string {".rb" },
265+ "extensions" : []string {".gemspec" },
266+ "enabled" : true ,
267+ "detected" : true ,
268+ },
269+ },
270+ })
271+ }))
272+ defer server .Close ()
273+
274+ // Patch CodacyApiBase to use the test server
275+ oldBase := CodacyApiBase
276+ CodacyApiBase = server .URL
277+ defer func () { CodacyApiBase = oldBase }()
278+
279+ apiTools := []Tool {
280+ {Uuid : "eslint-uuid" },
281+ {Uuid : "pylint-uuid" },
282+ {Uuid : "pmd-uuid" },
283+ }
284+ toolIDMap := map [string ]string {
285+ "eslint-uuid" : "eslint" ,
286+ "pylint-uuid" : "pylint" ,
287+ "pmd-uuid" : "pmd" ,
288+ }
289+
290+ err := CreateLanguagesConfigFile (apiTools , tempDir , toolIDMap , "test-token" , "gh" , "org" , "repo" )
291+ assert .NoError (t , err )
292+
293+ // Read and unmarshal the generated YAML
294+ data , err := os .ReadFile (tempDir + "/languages-config.yaml" )
295+ assert .NoError (t , err )
296+
297+ var config LanguagesConfig
298+ err = yaml .Unmarshal (data , & config )
299+ assert .NoError (t , err )
300+
301+ // Check that extensions are correct for each tool
302+ eslint := findTool (config .Tools , "eslint" )
303+ assert .ElementsMatch (t , []string {".js" , ".jsx" , ".vue" }, eslint .Extensions )
304+ pylint := findTool (config .Tools , "pylint" )
305+ assert .ElementsMatch (t , []string {".py" , ".testPy" }, pylint .Extensions )
306+ pmd := findTool (config .Tools , "pmd" )
307+ assert .ElementsMatch (t , []string {".cls" , ".app" , ".trigger" , ".scala" , ".rb" , ".gemspec" }, pmd .Extensions )
308+ }
309+
310+ func findTool (tools []ToolLanguageInfo , name string ) ToolLanguageInfo {
311+ for _ , t := range tools {
312+ if t .Name == name {
313+ return t
314+ }
315+ }
316+ return ToolLanguageInfo {}
317+ }
0 commit comments