Skip to content

Commit c970ca0

Browse files
fixed tests
1 parent 9e82b10 commit c970ca0

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
@@ -69,69 +69,87 @@ func (c *ConfigType) Runtimes() map[string]*plugins.RuntimeInfo {
6969
return c.runtimes
7070
}
7171

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

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

83-
// Update codacy.yaml with the new runtime
84-
if err := addRuntimeToCodacyYaml(name, info.Version); err != nil {
85-
return fmt.Errorf("failed to update codacy.yaml with runtime %s: %w", name, err)
81+
// Parse existing YAML or create new structure
82+
var config map[string]interface{}
83+
if len(content) > 0 {
84+
if err := yaml.Unmarshal(content, &config); err != nil {
85+
return fmt.Errorf("error parsing codacy.yaml: %w", err)
8686
}
87+
} else {
88+
config = make(map[string]interface{})
8789
}
8890

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

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

109-
// Prepare the new runtime string
110-
runtimeEntry := name + "@" + version
100+
// Check if runtime already exists and update it
111101
found := false
112-
for i, entry := range config.Runtimes {
113-
if strings.HasPrefix(entry, name+"@") {
114-
config.Runtimes[i] = runtimeEntry
115-
found = true
116-
break
102+
for i, r := range runtimes {
103+
if runtime, ok := r.(string); ok {
104+
if strings.Split(runtime, "@")[0] == name {
105+
runtimes[i] = runtimeEntry
106+
found = true
107+
break
108+
}
117109
}
118110
}
111+
112+
// Add new runtime if not found
119113
if !found {
120-
config.Runtimes = append(config.Runtimes, runtimeEntry)
114+
runtimes = append(runtimes, runtimeEntry)
121115
}
122116

123-
// Create .codacy directory if it doesn't exist
124-
if err := os.MkdirAll(".codacy", 0755); err != nil {
125-
return fmt.Errorf("failed to create .codacy directory: %w", err)
126-
}
117+
// Update config
118+
config["runtimes"] = runtimes
127119

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

137155
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)