|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFilterToolsByConfigUsage(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + inputTools []Tool |
| 13 | + expectedCount int |
| 14 | + expectedTools []string |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "tools with UsesConfigFile=true should be filtered out", |
| 18 | + inputTools: []Tool{ |
| 19 | + { |
| 20 | + Uuid: "eslint-uuid", |
| 21 | + Name: "eslint", |
| 22 | + Settings: struct { |
| 23 | + Enabled bool `json:"isEnabled"` |
| 24 | + UsesConfigFile bool `json:"hasConfigurationFile"` |
| 25 | + }{ |
| 26 | + Enabled: true, |
| 27 | + UsesConfigFile: true, |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + Uuid: "trivy-uuid", |
| 32 | + Name: "trivy", |
| 33 | + Settings: struct { |
| 34 | + Enabled bool `json:"isEnabled"` |
| 35 | + UsesConfigFile bool `json:"hasConfigurationFile"` |
| 36 | + }{ |
| 37 | + Enabled: true, |
| 38 | + UsesConfigFile: false, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + Uuid: "pylint-uuid", |
| 43 | + Name: "pylint", |
| 44 | + Settings: struct { |
| 45 | + Enabled bool `json:"isEnabled"` |
| 46 | + UsesConfigFile bool `json:"hasConfigurationFile"` |
| 47 | + }{ |
| 48 | + Enabled: true, |
| 49 | + UsesConfigFile: false, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + expectedCount: 2, |
| 54 | + expectedTools: []string{"trivy", "pylint"}, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "all tools using config should be filtered out", |
| 58 | + inputTools: []Tool{ |
| 59 | + { |
| 60 | + Uuid: "eslint-uuid", |
| 61 | + Name: "eslint", |
| 62 | + Settings: struct { |
| 63 | + Enabled bool `json:"isEnabled"` |
| 64 | + UsesConfigFile bool `json:"hasConfigurationFile"` |
| 65 | + }{ |
| 66 | + Enabled: true, |
| 67 | + UsesConfigFile: true, |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + Uuid: "trivy-uuid", |
| 72 | + Name: "trivy", |
| 73 | + Settings: struct { |
| 74 | + Enabled bool `json:"isEnabled"` |
| 75 | + UsesConfigFile bool `json:"hasConfigurationFile"` |
| 76 | + }{ |
| 77 | + Enabled: true, |
| 78 | + UsesConfigFile: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + expectedCount: 0, |
| 83 | + expectedTools: []string{}, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "no tools using config should all pass through", |
| 87 | + inputTools: []Tool{ |
| 88 | + { |
| 89 | + Uuid: "eslint-uuid", |
| 90 | + Name: "eslint", |
| 91 | + Settings: struct { |
| 92 | + Enabled bool `json:"isEnabled"` |
| 93 | + UsesConfigFile bool `json:"hasConfigurationFile"` |
| 94 | + }{ |
| 95 | + Enabled: true, |
| 96 | + UsesConfigFile: false, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + Uuid: "pylint-uuid", |
| 101 | + Name: "pylint", |
| 102 | + Settings: struct { |
| 103 | + Enabled bool `json:"isEnabled"` |
| 104 | + UsesConfigFile bool `json:"hasConfigurationFile"` |
| 105 | + }{ |
| 106 | + Enabled: true, |
| 107 | + UsesConfigFile: false, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + expectedCount: 2, |
| 112 | + expectedTools: []string{"eslint", "pylint"}, |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + for _, tt := range tests { |
| 117 | + t.Run(tt.name, func(t *testing.T) { |
| 118 | + // Use the actual function being tested |
| 119 | + result := FilterToolsByConfigUsage(tt.inputTools) |
| 120 | + |
| 121 | + // Verify the count matches |
| 122 | + assert.Equal(t, tt.expectedCount, len(result), |
| 123 | + "Expected %d tools after filtering, got %d", tt.expectedCount, len(result)) |
| 124 | + |
| 125 | + // Verify each expected tool is in the result |
| 126 | + for _, expectedTool := range tt.expectedTools { |
| 127 | + found := false |
| 128 | + for _, tool := range result { |
| 129 | + if tool.Name == expectedTool { |
| 130 | + found = true |
| 131 | + break |
| 132 | + } |
| 133 | + } |
| 134 | + assert.True(t, found, "Expected tool %s not found in filtered results", expectedTool) |
| 135 | + } |
| 136 | + |
| 137 | + // Verify no tools with UsesConfigFile=true are in the result |
| 138 | + for _, tool := range result { |
| 139 | + assert.False(t, tool.Settings.UsesConfigFile, |
| 140 | + "Tool %s with UsesConfigFile=true should not be in filtered results", tool.Name) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments