Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/features/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function resolveClean(
clean = []
}

if (clean.some((item) => path.resolve(item) === cwd)) {
if (clean.some((item) => path.resolve(cwd, item) === cwd)) {
throw new Error(
'Cannot clean the current working directory. Please specify a different path to clean option.',
)
Expand Down
44 changes: 44 additions & 0 deletions tests/clean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,50 @@ describe('clean', () => {
).rejects.toThrow('Cannot clean the current working directory')
})

test('should not clean a custom cwd', async (context) => {
const files = {
'pkg/index.ts': 'export const hello = "world"',
}

// `cwd` points at a package directory, as it does in workspace mode, while
// the process stays in the test directory.
await expect(
testBuild({
context,
files,
options: (cwd) => ({
cwd: path.join(cwd, 'pkg'),
clean: ['.'],
}),
}),
).rejects.toThrow('Cannot clean the current working directory')
})

test('should resolve clean patterns against a custom cwd', async (context) => {
const files = {
'pkg/index.ts': 'export const hello = "world"',
'pkg/pkg/old-file.js': 'old content',
}

const testDir = getTestDir(context.task)

await testBuild({
context,
files,
options: (cwd) => ({
cwd: path.join(cwd, 'pkg'),
clean: ['pkg'],
}),
snapshot: false,
})

// `pkg` is relative to the custom cwd, so it means `pkg/pkg`, not the cwd
const staleExists = await fsExists(
path.join(testDir, 'pkg', 'pkg', 'old-file.js'),
)
expect(staleExists).toBe(false)
})

test('should clean nested directories', async (context) => {
const files = {
'index.ts': 'export const hello = "world"',
Expand Down
Loading