Skip to content

Commit 2e18ff8

Browse files
fixed tests
1 parent 662cd30 commit 2e18ff8

File tree

2 files changed

+70
-48
lines changed

2 files changed

+70
-48
lines changed

config/config.go

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,69 +64,87 @@ func (c *ConfigType) Runtimes() map[string]*plugins.RuntimeInfo {
6464
return c.runtimes
6565
}
6666

67-
func (c *ConfigType) AddRuntimes(configs []plugins.RuntimeConfig) error {
68-
// Process the runtime configurations using the plugins.ProcessRuntimes function
69-
runtimeInfoMap, err := plugins.ProcessRuntimes(configs, c.runtimesDirectory)
70-
if err != nil {
71-
return err
72-
}
67+
func (c *ConfigType) addRuntimeToCodacyYaml(name string, version string) error {
68+
codacyPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
7369

74-
// Store the runtime information in the config
75-
for name, info := range runtimeInfoMap {
76-
c.runtimes[name] = info
70+
// Read existing content
71+
content, err := os.ReadFile(codacyPath)
72+
if err != nil && !os.IsNotExist(err) {
73+
return fmt.Errorf("error reading codacy.yaml: %w", err)
74+
}
7775

78-
// Update codacy.yaml with the new runtime
79-
if err := addRuntimeToCodacyYaml(name, info.Version); err != nil {
80-
return fmt.Errorf("failed to update codacy.yaml with runtime %s: %w", name, err)
76+
// Parse existing YAML or create new structure
77+
var config map[string]interface{}
78+
if len(content) > 0 {
79+
if err := yaml.Unmarshal(content, &config); err != nil {
80+
return fmt.Errorf("error parsing codacy.yaml: %w", err)
8181
}
82+
} else {
83+
config = make(map[string]interface{})
8284
}
8385

84-
return nil
85-
}
86-
87-
// addRuntimeToCodacyYaml adds or updates a runtime entry in .codacy/codacy.yaml as a YAML list
88-
func addRuntimeToCodacyYaml(name string, version string) error {
89-
codacyPath := ".codacy/codacy.yaml"
90-
91-
type CodacyConfig struct {
92-
Runtimes []string `yaml:"runtimes"`
93-
Tools []string `yaml:"tools"`
86+
// Get or create runtimes list
87+
runtimes, ok := config["runtimes"].([]interface{})
88+
if !ok {
89+
runtimes = make([]interface{}, 0)
9490
}
9591

96-
// Read existing config
97-
var config CodacyConfig
98-
if data, err := os.ReadFile(codacyPath); err == nil {
99-
if err := yaml.Unmarshal(data, &config); err != nil {
100-
return fmt.Errorf("failed to parse %s: %w", codacyPath, err)
101-
}
102-
}
92+
// Create runtime entry
93+
runtimeEntry := fmt.Sprintf("%s@%s", name, version)
10394

104-
// Prepare the new runtime string
105-
runtimeEntry := name + "@" + version
95+
// Check if runtime already exists and update it
10696
found := false
107-
for i, entry := range config.Runtimes {
108-
if strings.HasPrefix(entry, name+"@") {
109-
config.Runtimes[i] = runtimeEntry
110-
found = true
111-
break
97+
for i, r := range runtimes {
98+
if runtime, ok := r.(string); ok {
99+
if strings.Split(runtime, "@")[0] == name {
100+
runtimes[i] = runtimeEntry
101+
found = true
102+
break
103+
}
112104
}
113105
}
106+
107+
// Add new runtime if not found
114108
if !found {
115-
config.Runtimes = append(config.Runtimes, runtimeEntry)
109+
runtimes = append(runtimes, runtimeEntry)
116110
}
117111

118-
// Create .codacy directory if it doesn't exist
119-
if err := os.MkdirAll(".codacy", 0755); err != nil {
120-
return fmt.Errorf("failed to create .codacy directory: %w", err)
121-
}
112+
// Update config
113+
config["runtimes"] = runtimes
122114

123115
// Write back to .codacy/codacy.yaml
124116
yamlData, err := yaml.Marshal(config)
125117
if err != nil {
126-
return fmt.Errorf("failed to marshal config to YAML: %w", err)
118+
return fmt.Errorf("error marshaling codacy.yaml: %w", err)
127119
}
128-
if err := os.WriteFile(codacyPath, yamlData, 0644); err != nil {
129-
return fmt.Errorf("failed to write config to %s: %w", codacyPath, err)
120+
121+
// Ensure directory exists
122+
if err := os.MkdirAll(filepath.Dir(codacyPath), utils.DefaultDirPerms); err != nil {
123+
return fmt.Errorf("error creating .codacy directory: %w", err)
124+
}
125+
126+
if err := os.WriteFile(codacyPath, yamlData, utils.DefaultFilePerms); err != nil {
127+
return fmt.Errorf("error writing codacy.yaml: %w", err)
128+
}
129+
130+
return nil
131+
}
132+
133+
func (c *ConfigType) AddRuntimes(configs []plugins.RuntimeConfig) error {
134+
// Process the runtime configurations using the plugins.ProcessRuntimes function
135+
runtimeInfoMap, err := plugins.ProcessRuntimes(configs, c.runtimesDirectory)
136+
if err != nil {
137+
return err
138+
}
139+
140+
// Store the runtime information in the config
141+
for name, info := range runtimeInfoMap {
142+
c.runtimes[name] = info
143+
144+
// Update codacy.yaml with the new runtime
145+
if err := c.addRuntimeToCodacyYaml(name, info.Version); err != nil {
146+
return fmt.Errorf("failed to update codacy.yaml with runtime %s: %w", name, err)
147+
}
130148
}
131149

132150
return nil

config/tools-installer_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ func TestAddTools(t *testing.T) {
2020

2121
// Initialize config with test directories
2222
Config = ConfigType{
23-
toolsDirectory: tempDir,
24-
tools: make(map[string]*plugins.ToolInfo),
23+
toolsDirectory: tempDir,
24+
tools: make(map[string]*plugins.ToolInfo),
25+
runtimes: make(map[string]*plugins.RuntimeInfo),
26+
localCodacyDirectory: tempDir, // Add this to ensure .codacy/codacy.yaml is created in the temp dir
2527
}
2628

2729
// Create a list of tool configs for testing
@@ -94,8 +96,10 @@ func TestAddDownloadBasedTool(t *testing.T) {
9496

9597
// Initialize config with test directories
9698
Config = ConfigType{
97-
toolsDirectory: tempDir,
98-
tools: make(map[string]*plugins.ToolInfo),
99+
toolsDirectory: tempDir,
100+
tools: make(map[string]*plugins.ToolInfo),
101+
runtimes: make(map[string]*plugins.RuntimeInfo),
102+
localCodacyDirectory: tempDir, // Add this to ensure .codacy/codacy.yaml is created in the temp dir
99103
}
100104

101105
// Create a list of tool configs for testing

0 commit comments

Comments
 (0)