|
| 1 | +package deps |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "kusionstack.io/kusion/pkg/projectstack" |
| 11 | +) |
| 12 | + |
| 13 | +var workDir string |
| 14 | + |
| 15 | +func init() { |
| 16 | + cwd, err := os.Getwd() |
| 17 | + if err != nil { |
| 18 | + os.Exit(1) |
| 19 | + } |
| 20 | + workDir = filepath.Join(cwd, "testdata") |
| 21 | +} |
| 22 | + |
| 23 | +func TestNewCmdDeps(t *testing.T) { |
| 24 | + cmd := NewCmdDeps() |
| 25 | + if err := cmd.Execute(); err == nil { |
| 26 | + t.Fatal(err) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestDepsOptions_Validate(t *testing.T) { |
| 31 | + tCases := []struct { |
| 32 | + name string |
| 33 | + only string |
| 34 | + direct string |
| 35 | + workDir string |
| 36 | + focus []string |
| 37 | + ignore []string |
| 38 | + errMsg string |
| 39 | + }{ |
| 40 | + |
| 41 | + { |
| 42 | + name: "invalid output filter", |
| 43 | + errMsg: "invalid output downstream type. supported types: project, stack", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "invalid direct", |
| 47 | + only: "project", |
| 48 | + errMsg: "invalid output direction of the dependency inspection. supported directions: up, down", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "invalid workdir", |
| 52 | + only: "project", |
| 53 | + direct: "up", |
| 54 | + errMsg: "invalid work dir: stat : no such file or directory", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "invalid focus", |
| 58 | + only: "project", |
| 59 | + direct: "up", |
| 60 | + workDir: workDir, |
| 61 | + errMsg: "invalid focus paths. cannot be empty", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "invalid ignore", |
| 65 | + only: "project", |
| 66 | + direct: "up", |
| 67 | + workDir: workDir, |
| 68 | + focus: []string{"file.k"}, |
| 69 | + ignore: []string{"invalid_path.k"}, |
| 70 | + errMsg: fmt.Sprintf("invalid ignore path. need to be valid relative path from the workdir: stat %s: no such file or directory", filepath.Join(workDir, "invalid_path.k")), |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "valid", |
| 74 | + only: "project", |
| 75 | + direct: "up", |
| 76 | + workDir: workDir, |
| 77 | + focus: []string{"file.k"}, |
| 78 | + errMsg: "", |
| 79 | + }, |
| 80 | + } |
| 81 | + for _, tc := range tCases { |
| 82 | + t.Run(tc.name, func(t *testing.T) { |
| 83 | + opt := DepsOptions{ |
| 84 | + workDir: tc.workDir, |
| 85 | + Direct: tc.direct, |
| 86 | + Only: tc.only, |
| 87 | + Focus: tc.focus, |
| 88 | + Ignore: tc.ignore, |
| 89 | + } |
| 90 | + err := opt.Validate() |
| 91 | + if err != nil && err.Error() != tc.errMsg { |
| 92 | + t.Fatalf("wrong validate errMsg, actual: %s, expect: %s", err.Error(), tc.errMsg) |
| 93 | + } |
| 94 | + if err == nil && tc.errMsg != "" { |
| 95 | + t.Fatalf("wrong validate result: actual: validate success, expect err: %s", tc.errMsg) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func TestDepsOptions_Complete(t *testing.T) { |
| 102 | + tCases := []struct { |
| 103 | + name string |
| 104 | + args []string |
| 105 | + completed string |
| 106 | + }{ |
| 107 | + { |
| 108 | + name: "omit workdir in args", |
| 109 | + completed: filepath.Dir(workDir), |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "specify workdir in args", |
| 113 | + args: []string{"workdir path"}, |
| 114 | + completed: "workdir path", |
| 115 | + }, |
| 116 | + } |
| 117 | + for _, tc := range tCases { |
| 118 | + t.Run(tc.name, func(t *testing.T) { |
| 119 | + opt := NewDepsOptions() |
| 120 | + opt.Complete(tc.args) |
| 121 | + if opt.workDir != tc.completed { |
| 122 | + t.Fatalf("wrong completed workdir, actual: %s, expect: %s", opt.workDir, tc.completed) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestDepsOptions_Run(t *testing.T) { |
| 129 | + tCases := []struct { |
| 130 | + workDir string |
| 131 | + direct string |
| 132 | + only string |
| 133 | + }{ |
| 134 | + { |
| 135 | + workDir: workDir, |
| 136 | + direct: "up", |
| 137 | + }, |
| 138 | + { |
| 139 | + workDir: workDir, |
| 140 | + direct: "down", |
| 141 | + only: "project", |
| 142 | + }, |
| 143 | + } |
| 144 | + for _, tc := range tCases { |
| 145 | + t.Run(tc.direct, func(t *testing.T) { |
| 146 | + opt := DepsOptions{ |
| 147 | + workDir: tc.workDir, |
| 148 | + Direct: tc.direct, |
| 149 | + Only: tc.only, |
| 150 | + } |
| 151 | + if err := opt.Run(); err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestDepsOptions_Run2(t *testing.T) { |
| 159 | + opt := DepsOptions{ |
| 160 | + workDir: workDir, |
| 161 | + Direct: "up", |
| 162 | + Focus: []string{ |
| 163 | + "appops/projectC/dev/main.k", |
| 164 | + }, |
| 165 | + } |
| 166 | + err := opt.Run() |
| 167 | + if err != nil { |
| 168 | + t.Fatal(err) |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +var downstreamTestCases = []struct { |
| 173 | + name string |
| 174 | + focus []string |
| 175 | + downStreamProjects []string |
| 176 | + ignore []string |
| 177 | +}{ |
| 178 | + { |
| 179 | + name: "change base and entrance files", |
| 180 | + focus: []string{ |
| 181 | + "base/frontend/container/container_port.k", |
| 182 | + "appops/projectA/base/base.k", |
| 183 | + "appops/projectA/dev/main.k", |
| 184 | + "appops/projectA/dev/datafile.sql", |
| 185 | + }, |
| 186 | + downStreamProjects: []string{ |
| 187 | + "appops/projectA", |
| 188 | + "appops/projectB", |
| 189 | + "appops/projectC", |
| 190 | + }, |
| 191 | + }, |
| 192 | + { |
| 193 | + name: "change common base: container port", |
| 194 | + focus: []string{"base/frontend/container/container_port.k"}, |
| 195 | + downStreamProjects: []string{ |
| 196 | + "appops/projectA", |
| 197 | + "appops/projectB", |
| 198 | + "appops/projectC", |
| 199 | + }, |
| 200 | + }, |
| 201 | + { |
| 202 | + name: "change server render base", |
| 203 | + focus: []string{"base/render/server/server_render.k"}, |
| 204 | + downStreamProjects: []string{ |
| 205 | + "appops/projectA", |
| 206 | + "appops/projectC", |
| 207 | + }, |
| 208 | + }, |
| 209 | + { |
| 210 | + name: "change job render base", |
| 211 | + focus: []string{"base/render/job/job_render.k"}, |
| 212 | + downStreamProjects: []string{ |
| 213 | + "appops/projectB", |
| 214 | + }, |
| 215 | + }, |
| 216 | + { |
| 217 | + name: "ignore job render base", |
| 218 | + focus: []string{ |
| 219 | + "base/render/server/server_render.k", |
| 220 | + "base/render/job/job_render.k", |
| 221 | + }, |
| 222 | + ignore: []string{ |
| 223 | + "base/render/job/job_render.k", |
| 224 | + }, |
| 225 | + downStreamProjects: []string{ |
| 226 | + "appops/projectA", |
| 227 | + "appops/projectC", |
| 228 | + }, |
| 229 | + }, |
| 230 | + { |
| 231 | + name: "only change entrance files", |
| 232 | + focus: []string{ |
| 233 | + "appops/projectA/base/base.k", |
| 234 | + "appops/projectA/dev/main.k", |
| 235 | + "appops/projectB/dev/main.k", |
| 236 | + }, |
| 237 | + downStreamProjects: []string{ |
| 238 | + "appops/projectA", |
| 239 | + "appops/projectB", |
| 240 | + }, |
| 241 | + }, |
| 242 | + { |
| 243 | + name: "only change data files", |
| 244 | + focus: []string{ |
| 245 | + "appops/projectA/dev/datafile.sql", |
| 246 | + }, |
| 247 | + downStreamProjects: []string{ |
| 248 | + "appops/projectA", |
| 249 | + }, |
| 250 | + }, |
| 251 | + { |
| 252 | + name: "delete files", |
| 253 | + focus: []string{ |
| 254 | + "appops/projectC/dev/non_exist.sql", |
| 255 | + "base/frontend/non_exist/non_exist.k", |
| 256 | + }, |
| 257 | + downStreamProjects: []string{ |
| 258 | + "appops/projectC", |
| 259 | + }, |
| 260 | + }, |
| 261 | +} |
| 262 | + |
| 263 | +func TestFindDownStreams(t *testing.T) { |
| 264 | + projects, err := projectstack.FindAllProjectsFrom(workDir) |
| 265 | + if err != nil { |
| 266 | + t.Fatal(err) |
| 267 | + } |
| 268 | + for _, tc := range downstreamTestCases { |
| 269 | + t.Run(tc.name, func(t *testing.T) { |
| 270 | + result, err := findDownStreams(workDir, projects, toSet(tc.focus), toSet(tc.ignore), true) |
| 271 | + if err != nil { |
| 272 | + t.Fatal(err) |
| 273 | + } |
| 274 | + assert.ElementsMatch(t, result.toSlice(), tc.downStreamProjects, "test result mismatch") |
| 275 | + }) |
| 276 | + } |
| 277 | +} |
| 278 | + |
| 279 | +func BenchmarkDownStream(b *testing.B) { |
| 280 | + tc := downstreamTestCases[0] |
| 281 | + for i := 0; i < b.N; i++ { |
| 282 | + projects, err := projectstack.FindAllProjectsFrom(workDir) |
| 283 | + if err != nil { |
| 284 | + b.Fatal(err) |
| 285 | + } |
| 286 | + result, err := findDownStreams(workDir, projects, toSet(tc.focus), toSet(tc.ignore), true) |
| 287 | + if err != nil { |
| 288 | + b.Fatal(err) |
| 289 | + } |
| 290 | + assert.ElementsMatch(b, result.toSlice(), tc.downStreamProjects, "test result mismatch") |
| 291 | + } |
| 292 | +} |
0 commit comments