|
| 1 | +package build |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/paolo/flare-edge-cli/internal/domain/config" |
| 11 | + "github.com/paolo/flare-edge-cli/internal/infra/configstore" |
| 12 | + "github.com/paolo/flare-edge-cli/internal/infra/process" |
| 13 | + "github.com/paolo/flare-edge-cli/internal/infra/toolchain" |
| 14 | + "github.com/paolo/flare-edge-cli/internal/support/fs" |
| 15 | +) |
| 16 | + |
| 17 | +type fakeRunner struct { |
| 18 | + last process.Command |
| 19 | +} |
| 20 | + |
| 21 | +func (r *fakeRunner) Run(_ context.Context, cmd process.Command) (process.Result, error) { |
| 22 | + r.last = cmd |
| 23 | + |
| 24 | + output := "" |
| 25 | + for index := 0; index+1 < len(cmd.Args); index++ { |
| 26 | + if cmd.Args[index] == "-o" { |
| 27 | + output = cmd.Args[index+1] |
| 28 | + break |
| 29 | + } |
| 30 | + } |
| 31 | + if output == "" { |
| 32 | + return process.Result{}, nil |
| 33 | + } |
| 34 | + if !filepath.IsAbs(output) { |
| 35 | + output = filepath.Join(cmd.Dir, output) |
| 36 | + } |
| 37 | + if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil { |
| 38 | + return process.Result{}, err |
| 39 | + } |
| 40 | + if err := os.WriteFile(output, []byte("wasm"), 0o644); err != nil { |
| 41 | + return process.Result{}, err |
| 42 | + } |
| 43 | + return process.Result{}, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (r *fakeRunner) Stream(_ context.Context, _ process.Command, _, _ io.Writer) error { |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func TestWasmUsesStableArtifactPathAndRequiresArtifact(t *testing.T) { |
| 51 | + tmpDir := t.TempDir() |
| 52 | + projectDir := filepath.Join(tmpDir, "services", "orchestrator") |
| 53 | + filesystem := fs.New() |
| 54 | + store := configstore.New(filesystem) |
| 55 | + |
| 56 | + project := config.DefaultProject("orchestrator", "github.com/paolo/orchestrator", "main", "edge-http", config.DefaultCompatibilityDate, "") |
| 57 | + if err := store.SaveProject(projectDir, project); err != nil { |
| 58 | + t.Fatalf("save project: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + runner := &fakeRunner{} |
| 62 | + service := NewService(store, filesystem, runner, &toolchain.GoToolchain{}) |
| 63 | + |
| 64 | + result, err := service.Wasm(context.Background(), WasmOptions{ |
| 65 | + Path: projectDir, |
| 66 | + NoShim: true, |
| 67 | + }) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("build wasm: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + expectedArtifact := filepath.Join(projectDir, "dist", "app.wasm") |
| 73 | + if result.Artifact != expectedArtifact { |
| 74 | + t.Fatalf("unexpected artifact path: %q", result.Artifact) |
| 75 | + } |
| 76 | + outputIndex := -1 |
| 77 | + for index := 0; index+1 < len(runner.last.Args); index++ { |
| 78 | + if runner.last.Args[index] == "-o" { |
| 79 | + outputIndex = index + 1 |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + if outputIndex == -1 { |
| 84 | + t.Fatalf("expected -o flag in args: %v", runner.last.Args) |
| 85 | + } |
| 86 | + if !filepath.IsAbs(runner.last.Args[outputIndex]) { |
| 87 | + t.Fatalf("compiler output path should be absolute, got %q", runner.last.Args[outputIndex]) |
| 88 | + } |
| 89 | + if runner.last.Args[outputIndex] != expectedArtifact { |
| 90 | + t.Fatalf("unexpected compiler output path: %q", runner.last.Args[outputIndex]) |
| 91 | + } |
| 92 | + exists, err := filesystem.Exists(expectedArtifact) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("stat artifact: %v", err) |
| 95 | + } |
| 96 | + if !exists { |
| 97 | + t.Fatalf("expected artifact to exist at %s", expectedArtifact) |
| 98 | + } |
| 99 | +} |
0 commit comments