|
| 1 | +import { writeFile } from "node:fs/promises"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { describe, expect, it } from "vitest"; |
| 4 | +import { displayAutoConfigDetails } from "../../autoconfig/details"; |
| 5 | +import * as details from "../../autoconfig/details"; |
| 6 | +import { clearOutputFilePath } from "../../output"; |
| 7 | +import { mockConsoleMethods } from "../helpers/mock-console"; |
| 8 | +import { useMockIsTTY } from "../helpers/mock-istty"; |
| 9 | +import { runInTempDir } from "../helpers/run-in-tmp"; |
| 10 | +import { seed } from "../helpers/seed"; |
| 11 | +import type { Config } from "@cloudflare/workers-utils"; |
| 12 | + |
| 13 | +vi.mock("../../package-manager", () => ({ |
| 14 | + getPackageManager() { |
| 15 | + return { |
| 16 | + type: "npm", |
| 17 | + npx: "npx", |
| 18 | + }; |
| 19 | + }, |
| 20 | +})); |
| 21 | + |
| 22 | +describe("autoconfig details", () => { |
| 23 | + describe("getDetailsForAutoConfig()", () => { |
| 24 | + runInTempDir(); |
| 25 | + const { setIsTTY } = useMockIsTTY(); |
| 26 | + mockConsoleMethods(); |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + setIsTTY(true); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + vi.unstubAllGlobals(); |
| 34 | + clearOutputFilePath(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should set configured: true if a configPath exists", async () => { |
| 38 | + await expect( |
| 39 | + details.getDetailsForAutoConfig({ |
| 40 | + wranglerConfig: { configPath: "/tmp" } as Config, |
| 41 | + }) |
| 42 | + ).resolves.toMatchObject({ configured: true }); |
| 43 | + }); |
| 44 | + |
| 45 | + // Check that Astro is detected. We don't want to duplicate the tests of @netlify/build-info |
| 46 | + // by exhaustively checking every possible combination |
| 47 | + it("should perform basic framework detection", async () => { |
| 48 | + await writeFile( |
| 49 | + "package.json", |
| 50 | + JSON.stringify({ |
| 51 | + dependencies: { |
| 52 | + astro: "5", |
| 53 | + }, |
| 54 | + }) |
| 55 | + ); |
| 56 | + |
| 57 | + await expect(details.getDetailsForAutoConfig()).resolves.toMatchObject({ |
| 58 | + buildCommand: "astro build", |
| 59 | + configured: false, |
| 60 | + outputDir: "dist", |
| 61 | + packageJson: { |
| 62 | + dependencies: { |
| 63 | + astro: "5", |
| 64 | + }, |
| 65 | + }, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should bail when multiple frameworks are detected", async () => { |
| 70 | + await writeFile( |
| 71 | + "package.json", |
| 72 | + JSON.stringify({ |
| 73 | + dependencies: { |
| 74 | + astro: "5", |
| 75 | + gatsby: "5", |
| 76 | + }, |
| 77 | + }) |
| 78 | + ); |
| 79 | + |
| 80 | + await expect( |
| 81 | + details.getDetailsForAutoConfig() |
| 82 | + ).rejects.toThrowErrorMatchingInlineSnapshot( |
| 83 | + `[Error: Wrangler was unable to automatically configure your project to work with Cloudflare, since multiple frameworks were found: Astro, Gatsby]` |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should use npm build instead of framework build if present", async () => { |
| 88 | + await writeFile( |
| 89 | + "package.json", |
| 90 | + JSON.stringify({ |
| 91 | + scripts: { |
| 92 | + build: "echo build", |
| 93 | + }, |
| 94 | + dependencies: { |
| 95 | + astro: "5", |
| 96 | + }, |
| 97 | + }) |
| 98 | + ); |
| 99 | + |
| 100 | + await expect(details.getDetailsForAutoConfig()).resolves.toMatchObject({ |
| 101 | + buildCommand: "npm run build", |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + it("outputDir should be empty if nothing can be detected", async () => { |
| 106 | + await expect(details.getDetailsForAutoConfig()).resolves.toMatchObject({ |
| 107 | + outputDir: undefined, |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("outputDir should be set to cwd if an index.html file exists", async () => { |
| 112 | + await writeFile("index.html", `<h1>Hello World</h1>`); |
| 113 | + |
| 114 | + await expect(details.getDetailsForAutoConfig()).resolves.toMatchObject({ |
| 115 | + outputDir: process.cwd(), |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + it("outputDir should find first child directory with an index.html file", async () => { |
| 120 | + await seed({ |
| 121 | + "public/index.html": `<h1>Hello World</h1>`, |
| 122 | + "random/index.html": `<h1>Hello World</h1>`, |
| 123 | + }); |
| 124 | + |
| 125 | + await expect(details.getDetailsForAutoConfig()).resolves.toMatchObject({ |
| 126 | + outputDir: join(process.cwd(), "public"), |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + it("outputDir should prioritize the project directory over its child directories", async () => { |
| 131 | + await seed({ |
| 132 | + "index.html": `<h1>Hello World</h1>`, |
| 133 | + "public/index.html": `<h1>Hello World</h1>`, |
| 134 | + }); |
| 135 | + |
| 136 | + await expect(details.getDetailsForAutoConfig()).resolves.toMatchObject({ |
| 137 | + outputDir: process.cwd(), |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + describe("displayAutoConfigDetails()", () => { |
| 143 | + const std = mockConsoleMethods(); |
| 144 | + |
| 145 | + it("should cleanly handle a case in which no settings have been detected", () => { |
| 146 | + displayAutoConfigDetails({ |
| 147 | + configured: false, |
| 148 | + projectPath: process.cwd(), |
| 149 | + }); |
| 150 | + expect(std.out).toMatchInlineSnapshot( |
| 151 | + `"No Project Settings Auto-detected"` |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should display all the project settings provided by the details object", () => { |
| 156 | + displayAutoConfigDetails({ |
| 157 | + configured: false, |
| 158 | + projectPath: process.cwd(), |
| 159 | + framework: { name: "Astro", configured: false, configure: () => ({}) }, |
| 160 | + buildCommand: "astro build", |
| 161 | + outputDir: "dist", |
| 162 | + }); |
| 163 | + expect(std.out).toMatchInlineSnapshot(` |
| 164 | + "Auto-detected Project Settings: |
| 165 | + - Framework: Astro |
| 166 | + - Build Command: astro build |
| 167 | + - Output Directory: dist |
| 168 | + " |
| 169 | + `); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should omit the framework entry when they it is not part of the details object", () => { |
| 173 | + displayAutoConfigDetails({ |
| 174 | + configured: false, |
| 175 | + projectPath: process.cwd(), |
| 176 | + buildCommand: "npm run build", |
| 177 | + outputDir: "dist", |
| 178 | + }); |
| 179 | + expect(std.out).toMatchInlineSnapshot(` |
| 180 | + "Auto-detected Project Settings: |
| 181 | + - Build Command: npm run build |
| 182 | + - Output Directory: dist |
| 183 | + " |
| 184 | + `); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should omit the framework and build command entries when they are not part of the details object", () => { |
| 188 | + displayAutoConfigDetails({ |
| 189 | + configured: false, |
| 190 | + projectPath: process.cwd(), |
| 191 | + outputDir: "dist", |
| 192 | + }); |
| 193 | + expect(std.out).toMatchInlineSnapshot(` |
| 194 | + "Auto-detected Project Settings: |
| 195 | + - Output Directory: dist |
| 196 | + " |
| 197 | + `); |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments