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