|
| 1 | +package nodejs |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "google3/security/safeopen/safeopen" |
| 10 | + "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack" |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | +) |
| 13 | + |
| 14 | +func TestSvelteKitStartCommand(t *testing.T) { |
| 15 | + testsCases := []struct { |
| 16 | + name string |
| 17 | + configExists bool |
| 18 | + buildExists bool |
| 19 | + want []string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "no config or build", |
| 23 | + want: nil, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "no config", |
| 27 | + buildExists: true, |
| 28 | + want: nil, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "no build", |
| 32 | + configExists: true, |
| 33 | + want: nil, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "sveltekit app", |
| 37 | + buildExists: true, |
| 38 | + configExists: true, |
| 39 | + want: []string{"node", "build/index.js"}, |
| 40 | + }, |
| 41 | + } |
| 42 | + for _, tc := range testsCases { |
| 43 | + t.Run(tc.name, func(t *testing.T) { |
| 44 | + home := t.TempDir() |
| 45 | + if tc.configExists { |
| 46 | + _, err := safeopen.CreateBeneath(home, "svelte.config.js") |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("failed to create server.js: %v", err) |
| 49 | + } |
| 50 | + } |
| 51 | + if tc.buildExists { |
| 52 | + dir := filepath.Join(home, "build") |
| 53 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 54 | + t.Fatalf("failed to create build directory: %v", err) |
| 55 | + } |
| 56 | + _, err := safeopen.CreateBeneath(dir, "index.js") |
| 57 | + if err != nil { |
| 58 | + t.Fatalf("failed to create index.js: %v", err) |
| 59 | + } |
| 60 | + } |
| 61 | + ctx := gcpbuildpack.NewContext(gcpbuildpack.WithApplicationRoot(home)) |
| 62 | + |
| 63 | + got, err := SvelteKitStartCommand(ctx) |
| 64 | + if err != nil { |
| 65 | + t.Fatalf("SvelteKitStartCommand() got error: %v", err) |
| 66 | + } |
| 67 | + if diff := cmp.Diff(tc.want, got); diff != "" { |
| 68 | + t.Errorf("SvelteKitStartCommand() mismatch (-want +got):\n%s", diff) |
| 69 | + } |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestDetectSvelteKitAutoAdapter(t *testing.T) { |
| 75 | + testsCases := []struct { |
| 76 | + name string |
| 77 | + pjs string |
| 78 | + want bool |
| 79 | + }{ |
| 80 | + { |
| 81 | + name: "with sveltekit auto adapter", |
| 82 | + pjs: `{ |
| 83 | + "devDependencies": { |
| 84 | + "@sveltejs/adapter-auto": "^3.0.0" |
| 85 | + } |
| 86 | + }`, |
| 87 | + want: true, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "with two sveltekit adapters", |
| 91 | + pjs: `{ |
| 92 | + "devDependencies": { |
| 93 | + "@sveltejs/adapter-auto": "^3.0.0", |
| 94 | + "@sveltejs/adapter-node": "^5.0.1" |
| 95 | + } |
| 96 | + }`, |
| 97 | + want: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "with no sveltekit adapters", |
| 101 | + pjs: `{ |
| 102 | + "scripts": { |
| 103 | + "dev": "vite dev", |
| 104 | + "build": "vite build", |
| 105 | + "preview": "vite preview", |
| 106 | + "check": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json", |
| 107 | + "check:watch": "svelte-kit sync && svelte-check --tsconfig ./jsconfig.json --watch" |
| 108 | + } |
| 109 | + }`, |
| 110 | + want: false, |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "no scripts", |
| 114 | + pjs: `{}`, |
| 115 | + want: false, |
| 116 | + }, |
| 117 | + } |
| 118 | + for _, tc := range testsCases { |
| 119 | + t.Run(tc.name, func(t *testing.T) { |
| 120 | + var pjs *PackageJSON = nil |
| 121 | + if tc.pjs != "" { |
| 122 | + if err := json.Unmarshal([]byte(tc.pjs), &pjs); err != nil { |
| 123 | + t.Fatalf("failed to unmarshal package.json: %s, error: %v", tc.pjs, err) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if got := DetectSvelteKitAutoAdapter(pjs); got != tc.want { |
| 128 | + t.Errorf("DetectSvelteKitAutoAdapter() = %v, want %v", got, tc.want) |
| 129 | + } |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments