@@ -220,7 +220,7 @@ func TestInitCommand_NoToken(t *testing.T) {
220220 cliLocalMode := len (currentInitFlags .ApiToken ) == 0
221221 if cliLocalMode {
222222 noTools := []domain.Tool {}
223- err := configsetup .CreateConfigurationFiles (noTools , cliLocalMode )
223+ err := configsetup .CreateConfigurationFiles (noTools , cliLocalMode , currentInitFlags )
224224 assert .NoError (t , err , "CreateConfigurationFiles should not return an error" )
225225 if err := configsetup .BuildDefaultConfigurationFiles (toolsConfigDir , currentInitFlags ); err != nil {
226226 t .Fatalf ("Failed to build default configuration files: %v" , err )
@@ -255,4 +255,91 @@ func TestInitCommand_NoToken(t *testing.T) {
255255 _ , err := os .Stat (filePath )
256256 assert .NoError (t , err , "Expected config file %s to be created at %s" , file , filePath )
257257 }
258+
259+ cliConfigPath := filepath .Join (codacyDir , "cli-config.yaml" )
260+ cliConfigFileContent , err := os .ReadFile (cliConfigPath )
261+ assert .NoError (t , err , "Expected cli-config.yaml file to be created at %s" , cliConfigPath )
262+
263+ assert .Equal (t , cliConfigFileContent , "mode: remote" )
264+ }
265+
266+ func TestInitCommand_WithToken (t * testing.T ) {
267+ tempDir := t .TempDir ()
268+ originalWD , err := os .Getwd ()
269+ assert .NoError (t , err , "Failed to get current working directory" )
270+ defer os .Chdir (originalWD )
271+
272+ // Use the real plugins/tools/semgrep/rules.yaml file
273+ rulesPath := filepath .Join (".." , "plugins" , "tools" , "semgrep" , "rules.yaml" )
274+ if _ , err := os .Stat (rulesPath ); os .IsNotExist (err ) {
275+ t .Skipf ("plugins/tools/semgrep/rules.yaml not found at %s; skipping test" , rulesPath )
276+ }
277+
278+ // Change to the temp directory to simulate a new project
279+ err = os .Chdir (tempDir )
280+ assert .NoError (t , err , "Failed to change working directory to tempDir" )
281+
282+ // Simulate running init with no token
283+ currentInitFlags := domain.InitFlags {
284+ ApiToken : "token" ,
285+ Provider : "gh" ,
286+ Organization : "acme" ,
287+ Repository : "acme-repo" ,
288+ }
289+
290+ // Call the Run logic from initCmd
291+ if err := config .Config .CreateLocalCodacyDir (); err != nil {
292+ t .Fatalf ("Failed to create local codacy directory: %v" , err )
293+ }
294+
295+ toolsConfigDir := config .Config .ToolsConfigDirectory ()
296+ if err := os .MkdirAll (toolsConfigDir , constants .DefaultDirPerms ); err != nil {
297+ t .Fatalf ("Failed to create tools-configs directory: %v" , err )
298+ }
299+
300+ cliLocalMode := len (currentInitFlags .ApiToken ) == 0
301+ if cliLocalMode {
302+ noTools := []domain.Tool {}
303+ err := configsetup .CreateConfigurationFiles (noTools , cliLocalMode , currentInitFlags )
304+ assert .NoError (t , err , "CreateConfigurationFiles should not return an error" )
305+ if err := configsetup .BuildDefaultConfigurationFiles (toolsConfigDir , currentInitFlags ); err != nil {
306+ t .Fatalf ("Failed to build default configuration files: %v" , err )
307+ }
308+ if err := configsetup .CreateLanguagesConfigFileLocal (toolsConfigDir ); err != nil {
309+ t .Fatalf ("Failed to create languages config file: %v" , err )
310+ }
311+ }
312+
313+ // Assert that the expected config files are created
314+ codacyDir := config .Config .LocalCodacyDirectory ()
315+ expectedFiles := []string {
316+ filepath .Join ("tools-configs" , "eslint.config.mjs" ),
317+ filepath .Join ("tools-configs" , "trivy.yaml" ),
318+ filepath .Join ("tools-configs" , "ruleset.xml" ),
319+ filepath .Join ("tools-configs" , "pylint.rc" ),
320+ filepath .Join ("tools-configs" , "analysis_options.yaml" ),
321+ filepath .Join ("tools-configs" , "semgrep.yaml" ),
322+ filepath .Join ("tools-configs" , "lizard.yaml" ),
323+ "codacy.yaml" ,
324+ "cli-config.yaml" ,
325+ filepath .Join ("tools-configs" , "languages-config.yaml" ),
326+ ".gitignore" ,
327+ }
328+
329+ for _ , file := range expectedFiles {
330+ filePath := filepath .Join (codacyDir , file )
331+ if file == ".gitignore" {
332+ filePath = filepath .Join (config .Config .LocalCodacyDirectory (), file )
333+ }
334+
335+ _ , err := os .Stat (filePath )
336+ assert .NoError (t , err , "Expected config file %s to be created at %s" , file , filePath )
337+ }
338+
339+ cliConfigPath := filepath .Join (codacyDir , "cli-config.yaml" )
340+ cliConfigFileContent , err := os .ReadFile (cliConfigPath )
341+ assert .NoError (t , err , "Expected cli-config.yaml file to be created at %s" , cliConfigPath )
342+
343+ assert .Equal (t , cliConfigFileContent , "mode: remote\n provider: gh\n organizg4g4ation: acme\n repository: acme-repo" )
344+
258345}
0 commit comments