|
1 | | -import { compareSemver } from "@opennextjs/aws/build/helper.js"; |
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +import { |
| 5 | + compareSemver, |
| 6 | + copyCustomFiles, |
| 7 | +} from "@opennextjs/aws/build/helper.js"; |
| 8 | +import logger from "@opennextjs/aws/logger.js"; |
| 9 | +import mockFs from "mock-fs"; |
| 10 | +import { vi } from "vitest"; |
2 | 11 |
|
3 | 12 | // We don't need to test canary versions, they are stripped out |
4 | 13 | describe("compareSemver", () => { |
@@ -65,3 +74,175 @@ describe("compareSemver", () => { |
65 | 74 | expect(() => compareSemver("14.0.0", "!=" as any, "14.0.0")).toThrow(); |
66 | 75 | }); |
67 | 76 | }); |
| 77 | + |
| 78 | +const outputFolder = ".open-next/server-functions/default"; |
| 79 | + |
| 80 | +describe("copyFiles", () => { |
| 81 | + beforeEach(() => { |
| 82 | + mockFs({ |
| 83 | + "this/is/a/fake/dir": { |
| 84 | + "some-file24214.txt": "some content", |
| 85 | + "another-fil321313e.txt": "another content", |
| 86 | + "empty-file321441.txt": "", |
| 87 | + "important-js": { |
| 88 | + "another-important.js": "console.log('important!')", |
| 89 | + }, |
| 90 | + }, |
| 91 | + "this/is/a/real/dir": { |
| 92 | + "randomfile.txt": "some content", |
| 93 | + "another-dirfdsf": { |
| 94 | + "another-filedsfdsf.txt": "another content", |
| 95 | + }, |
| 96 | + "empty-file.txtfdsf": "", |
| 97 | + "imporant-files": { |
| 98 | + "important.js": "console.log('important!')", |
| 99 | + "super-important.js": "console.log('super important!')", |
| 100 | + }, |
| 101 | + }, |
| 102 | + [`${outputFolder}/server`]: { |
| 103 | + "index.mjs": "globalThis.process.env = {}", |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + vi.spyOn(fs, "copyFileSync"); |
| 108 | + vi.spyOn(fs, "mkdirSync"); |
| 109 | + vi.spyOn(fs, "readFileSync"); |
| 110 | + }); |
| 111 | + |
| 112 | + afterAll(() => { |
| 113 | + mockFs.restore(); |
| 114 | + vi.restoreAllMocks(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should work with a glob, dstPath should become a directory", () => { |
| 118 | + copyCustomFiles( |
| 119 | + [ |
| 120 | + { |
| 121 | + srcPath: "**/*.js", |
| 122 | + dstPath: "functions", |
| 123 | + }, |
| 124 | + ], |
| 125 | + outputFolder, |
| 126 | + ); |
| 127 | + |
| 128 | + const dstDir = path.join(outputFolder, "functions"); |
| 129 | + expect(fs.copyFileSync).toHaveBeenCalledTimes(3); |
| 130 | + expect(fs.mkdirSync).toHaveBeenCalledWith(dstDir, { recursive: true }); |
| 131 | + expect(fs.mkdirSync).toHaveBeenCalledTimes(1); |
| 132 | + |
| 133 | + expect(fs.readdirSync(dstDir)).toEqual([ |
| 134 | + "another-important.js", |
| 135 | + "important.js", |
| 136 | + "super-important.js", |
| 137 | + ]); |
| 138 | + |
| 139 | + expect( |
| 140 | + fs.readFileSync(path.join(dstDir, "important.js")).toString(), |
| 141 | + ).toMatchInlineSnapshot(`"console.log('important!')"`); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should copy a single file when srcPath matches one file", () => { |
| 145 | + copyCustomFiles( |
| 146 | + [ |
| 147 | + { |
| 148 | + srcPath: "this/is/a/real/dir/randomfile.txt", |
| 149 | + dstPath: "randomfolder/randomfile.txt", |
| 150 | + }, |
| 151 | + ], |
| 152 | + outputFolder, |
| 153 | + ); |
| 154 | + |
| 155 | + const dstDir = path.join(outputFolder, "randomfolder"); |
| 156 | + expect(fs.mkdirSync).toHaveBeenCalledWith(dstDir, { recursive: true }); |
| 157 | + expect(fs.mkdirSync).toHaveBeenCalledTimes(1); |
| 158 | + |
| 159 | + expect(fs.copyFileSync).toHaveBeenCalledTimes(1); |
| 160 | + expect(fs.copyFileSync).toHaveBeenCalledWith( |
| 161 | + "this/is/a/real/dir/randomfile.txt", |
| 162 | + path.join(outputFolder, "randomfolder/randomfile.txt"), |
| 163 | + ); |
| 164 | + |
| 165 | + expect( |
| 166 | + fs.readFileSync(path.join(outputFolder, "randomfolder/randomfile.txt"), { |
| 167 | + encoding: "utf-8", |
| 168 | + }), |
| 169 | + ).toMatchInlineSnapshot(`"some content"`); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should work with a glob in a sub directory", () => { |
| 173 | + copyCustomFiles( |
| 174 | + [ |
| 175 | + { |
| 176 | + srcPath: "this/is/a/real/dir/imporant-files/**/*.js", |
| 177 | + dstPath: "super/functions", |
| 178 | + }, |
| 179 | + ], |
| 180 | + outputFolder, |
| 181 | + ); |
| 182 | + |
| 183 | + expect(fs.mkdirSync).toHaveBeenCalledWith( |
| 184 | + path.join(outputFolder, "super/functions"), |
| 185 | + { recursive: true }, |
| 186 | + ); |
| 187 | + expect(fs.mkdirSync).toHaveBeenCalledTimes(1); |
| 188 | + |
| 189 | + expect(fs.copyFileSync).toHaveBeenCalledTimes(2); |
| 190 | + expect(fs.copyFileSync).toHaveBeenCalledWith( |
| 191 | + "this/is/a/real/dir/imporant-files/important.js", |
| 192 | + path.join(outputFolder, "super/functions/important.js"), |
| 193 | + ); |
| 194 | + expect(fs.copyFileSync).toHaveBeenCalledWith( |
| 195 | + "this/is/a/real/dir/imporant-files/super-important.js", |
| 196 | + path.join(outputFolder, "super/functions/super-important.js"), |
| 197 | + ); |
| 198 | + |
| 199 | + expect(fs.readdirSync(path.join(outputFolder, "super/functions"))).toEqual([ |
| 200 | + "important.js", |
| 201 | + "super-important.js", |
| 202 | + ]); |
| 203 | + expect( |
| 204 | + fs.readFileSync( |
| 205 | + path.join(outputFolder, "super/functions/super-important.js"), |
| 206 | + { encoding: "utf-8" }, |
| 207 | + ), |
| 208 | + ).toMatchInlineSnapshot(`"console.log('super important!')"`); |
| 209 | + }); |
| 210 | + it("should warn when file already exists", () => { |
| 211 | + const logSpy = vi.spyOn(logger, "warn"); |
| 212 | + |
| 213 | + copyCustomFiles( |
| 214 | + [ |
| 215 | + { |
| 216 | + srcPath: "this/is/a/fake/dir/some-file24214.txt", |
| 217 | + dstPath: "server/index.mjs", |
| 218 | + }, |
| 219 | + ], |
| 220 | + outputFolder, |
| 221 | + ); |
| 222 | + |
| 223 | + const fullDstPath = path.join(outputFolder, "server/index.mjs"); |
| 224 | + expect(logSpy).toHaveBeenCalledWith( |
| 225 | + `File already exists: ${fullDstPath}. It will be overwritten.`, |
| 226 | + ); |
| 227 | + logSpy.mockRestore(); |
| 228 | + }); |
| 229 | + it("should warn when no files are found", () => { |
| 230 | + const logSpy = vi.spyOn(logger, "warn"); |
| 231 | + const srcPath = "path/to/dir/does-not-exist.txt"; |
| 232 | + |
| 233 | + copyCustomFiles( |
| 234 | + [ |
| 235 | + { |
| 236 | + srcPath: srcPath, |
| 237 | + dstPath: "server/index.mjs", |
| 238 | + }, |
| 239 | + ], |
| 240 | + outputFolder, |
| 241 | + ); |
| 242 | + |
| 243 | + expect(logSpy).toHaveBeenCalledWith( |
| 244 | + `No files found for pattern: ${srcPath}`, |
| 245 | + ); |
| 246 | + logSpy.mockRestore(); |
| 247 | + }); |
| 248 | +}); |
0 commit comments