|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/libops/sitectl/pkg/plugin" |
| 11 | +) |
| 12 | + |
| 13 | +func TestProjectDetectClaimsPlainDrupalProject(t *testing.T) { |
| 14 | + projectDir := t.TempDir() |
| 15 | + writeFileForTest(t, filepath.Join(projectDir, "docker-compose.yml"), "services:\n drupal:\n image: drupal:latest\n") |
| 16 | + writeFileForTest(t, filepath.Join(projectDir, "composer.json"), `{"require": {}}`) |
| 17 | + |
| 18 | + result := runDrupalProjectDetectForTest(t, projectDir) |
| 19 | + if !result.Claimed { |
| 20 | + t.Fatalf("expected Drupal project detection to claim %s", projectDir) |
| 21 | + } |
| 22 | + if result.Plugin != "drupal" { |
| 23 | + t.Fatalf("expected drupal claim, got %#v", result) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestProjectDetectDeclinesISLEServices(t *testing.T) { |
| 28 | + projectDir := t.TempDir() |
| 29 | + writeFileForTest(t, filepath.Join(projectDir, "docker-compose.yml"), "services:\n alpaca:\n image: libops/alpaca:2\n drupal:\n image: islandora/drupal:main\n") |
| 30 | + writeFileForTest(t, filepath.Join(projectDir, "composer.json"), `{"require": {}}`) |
| 31 | + |
| 32 | + result := runDrupalProjectDetectForTest(t, projectDir) |
| 33 | + if result.Claimed { |
| 34 | + t.Fatalf("expected Drupal project detection to decline ISLE services, got %#v", result) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func runDrupalProjectDetectForTest(t *testing.T, projectDir string) projectDetectResultForTest { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + sdk := plugin.NewSDK(plugin.Metadata{Name: "drupal"}) |
| 42 | + if err := RegisterCommands(sdk); err != nil { |
| 43 | + t.Fatalf("RegisterCommands() error = %v", err) |
| 44 | + } |
| 45 | + |
| 46 | + req, err := plugin.NewProjectDetectRequest(projectDir) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("NewProjectDetectRequest() error = %v", err) |
| 49 | + } |
| 50 | + requestData, err := json.Marshal(req) |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("Marshal(RPCRequest) error = %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + cmd := sdk.GetRPCCommand() |
| 56 | + var stdout bytes.Buffer |
| 57 | + cmd.SetIn(bytes.NewReader(requestData)) |
| 58 | + cmd.SetOut(&stdout) |
| 59 | + if err := cmd.Execute(); err != nil { |
| 60 | + t.Fatalf("Execute() error = %v", err) |
| 61 | + } |
| 62 | + |
| 63 | + var resp plugin.RPCResponse |
| 64 | + if err := json.Unmarshal(stdout.Bytes(), &resp); err != nil { |
| 65 | + t.Fatalf("Unmarshal(RPCResponse) error = %v: %s", err, stdout.String()) |
| 66 | + } |
| 67 | + if !resp.OK { |
| 68 | + t.Fatalf("project.detect failed: %+v", resp.Error) |
| 69 | + } |
| 70 | + var result projectDetectResultForTest |
| 71 | + if err := json.Unmarshal(resp.Result, &result); err != nil { |
| 72 | + t.Fatalf("Unmarshal(project detect result) error = %v", err) |
| 73 | + } |
| 74 | + return result |
| 75 | +} |
| 76 | + |
| 77 | +type projectDetectResultForTest struct { |
| 78 | + Claimed bool `json:"claimed"` |
| 79 | + Plugin string `json:"plugin"` |
| 80 | + ProjectDir string `json:"project_dir"` |
| 81 | + Reason string `json:"reason"` |
| 82 | +} |
| 83 | + |
| 84 | +func writeFileForTest(t *testing.T, path, contents string) { |
| 85 | + t.Helper() |
| 86 | + if err := os.WriteFile(path, []byte(contents), 0o644); err != nil { |
| 87 | + t.Fatalf("WriteFile(%s) error = %v", path, err) |
| 88 | + } |
| 89 | +} |
0 commit comments