Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions src/utils/deploy/upload-source-zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,32 @@ interface UploadSourceZipOptions {
}

const DEFAULT_IGNORE_PATTERNS = [
'node_modules*',
'.git*',
'.netlify*',
'.next*',
'dist*',
'build*',
'.nuxt*',
'.output*',
'.vercel*',
'__pycache__*',
'.venv*',
'node_modules/*',
'.git',
'.git/*',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
'.git-credentials',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
'.git-credentials.lock',
'.gitconfig',
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
'.gitconfig.lock',
'.gitcookies',
'.netlify/*',
'.next/*',
'dist/*',
'build/*',
'.nuxt/*',
'.output/*',
'.vercel/*',
'__pycache__/*',
'.venv/*',
'.env',
'.DS_Store',
'Thumbs.db',
'*.log',
'.nyc_output*',
'coverage*',
'.cache*',
'.tmp*',
'.temp*',
'.nyc_output/*',
'coverage/*',
'.cache/*',
'.tmp/*',
'.temp/*',
]

const isExecaError = (error: unknown): error is ExecaError => error instanceof Error && 'command' in error
Expand Down
111 changes: 111 additions & 0 deletions tests/unit/utils/deploy/upload-source-zip-contents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'

import execa from 'execa'
import fetch from 'node-fetch'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'

import { uploadSourceZip } from '../../../../src/utils/deploy/upload-source-zip.js'

vi.mock('node-fetch', () => ({
default: vi.fn(),
}))

vi.mock('../../../../src/utils/command-helpers.js', () => ({
log: vi.fn(),
warn: vi.fn(),
}))

const zippedPaths = async (workDir: string, files: string[]) => {
const sourceDir = join(workDir, 'source')
for (const file of files) {
await mkdir(dirname(join(sourceDir, file)), { recursive: true })
await writeFile(join(sourceDir, file), '')
}

let uploadedZip: Buffer | undefined
vi.mocked(fetch).mockImplementation((_url, init) => {
uploadedZip = init?.body as Buffer
return Promise.resolve({ ok: true, status: 200, statusText: 'OK' } as Awaited<ReturnType<typeof fetch>>)
})

await uploadSourceZip({
sourceDir,
uploadUrl: 'https://s3.example.com/upload-url',
filename: 'workspace-snapshots/source.zip',
})

const zipPath = join(workDir, 'uploaded.zip')
await writeFile(zipPath, uploadedZip ?? Buffer.alloc(0))
const { stdout } = await execa('unzip', ['-Z1', zipPath])
return stdout.split('\n').filter((path) => !path.endsWith('/'))
}

describe.skipIf(process.platform === 'win32')('uploadSourceZip contents', () => {
let workDir: string

beforeEach(async () => {
workDir = await mkdtemp(join(tmpdir(), 'upload-source-zip-'))
})

afterEach(async () => {
await rm(workDir, { recursive: true, force: true })
})

test('keeps root files that share a prefix with an ignored folder', async () => {
const paths = await zippedPaths(workDir, [
'index.html',
'build.cjs',
'build.sh',
'builder.config.js',
'distribute.js',
'coverage.md',
'.gitignore',
'.github/workflows/ci.yml',
'src/build/index.js',
])

expect(paths.sort()).toEqual(
[
'index.html',
'build.cjs',
'build.sh',
'builder.config.js',
'distribute.js',
'coverage.md',
'.gitignore',
'.github/workflows/ci.yml',
'src/build/index.js',
].sort(),
)
})

test('leaves out ignored folders and files', async () => {
const paths = await zippedPaths(workDir, [
'index.html',
'node_modules/pkg/index.js',
'.git/HEAD',
'.git-credentials',
'.git-credentials.lock',
'.gitconfig',
'.gitconfig.lock',
'.gitcookies',
'.netlify/state.json',
'.next/server.js',
'dist/app.js',
'build/app.js',
'coverage/lcov.info',
'.env',
'debug.log',
])

expect(paths).toEqual(['index.html'])
})

test('leaves out the .git file of a linked worktree', async () => {
const paths = await zippedPaths(workDir, ['index.html', '.git', '.gitignore'])

expect(paths.sort()).toEqual(['.gitignore', 'index.html'])
})
})
2 changes: 1 addition & 1 deletion tests/unit/utils/deploy/upload-source-zip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ describe('uploadSourceZip', () => {

expect(mockExeca.default).toHaveBeenCalledWith(
'zip',
expect.arrayContaining(['-x', 'node_modules*', '-x', '.git*', '-x', '.netlify*', '-x', '.env']),
expect.arrayContaining(['-x', 'node_modules/*', '-x', '.git/*', '-x', '.netlify/*', '-x', '.env']),
expect.objectContaining({ cwd: '/test/source' }),
)
})
Expand Down
Loading