Skip to content

Commit

Permalink
Test for new file creation in the PostCSS plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Oct 30, 2024
1 parent 6c9da18 commit 8bd9176
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 26 deletions.
62 changes: 36 additions & 26 deletions integrations/postcss/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -947,32 +947,42 @@ test(
])

// Creating new files in the "root" of auto source detected folders
// await fs.write(
// 'project-b/new-file.html',
// html`<div class="[.created_&]:content-['project-b/new-file.html']"></div>`,
// )
// await fs.write(
// 'project-b/new-folder/new-file.html',
// html`<div class="[.created_&]:content-['project-b/new-folder/new-file.html']"></div>`,
// )
// await fs.write(
// 'project-c/new-file.html',
// html`<div class="[.created_&]:content-['project-c/new-file.html']"></div>`,
// )
// await fs.write(
// 'project-c/new-folder/new-file.html',
// html`<div class="[.created_&]:content-['project-c/new-folder/new-file.html']"></div>`,
// )

// await fs.write('project-a/src/index.css', await fs.read('project-a/src/index.css'))
// await new Promise((resolve) => setTimeout(resolve, 1000))

// await fs.expectFileToContain('./project-a/dist/out.css', [
// candidate`[.created_&]:content-['project-b/new-file.html']`,
// candidate`[.created_&]:content-['project-b/new-folder/new-file.html']`,
// candidate`[.created_&]:content-['project-c/new-file.html']`,
// candidate`[.created_&]:content-['project-c/new-folder/new-file.html']`,
// ])
// We need to create the files and *then* update them because postcss-cli
// does not pick up new files — only changes to existing files.
await fs.create([
'project-b/new-file.html',
'project-b/new-folder/new-file.html',
'project-c/new-file.html',
'project-c/new-folder/new-file.html',
])

// If we don't wait writes will be coalesced into a "add" event which
// isn't picked up by postcss-cli.
await new Promise((resolve) => setTimeout(resolve, 100))

await fs.write(
'project-b/new-file.html',
html`<div class="[.created_&]:content-['project-b/new-file.html']"></div>`,
)
await fs.write(
'project-b/new-folder/new-file.html',
html`<div class="[.created_&]:content-['project-b/new-folder/new-file.html']"></div>`,
)
await fs.write(
'project-c/new-file.html',
html`<div class="[.created_&]:content-['project-c/new-file.html']"></div>`,
)
await fs.write(
'project-c/new-folder/new-file.html',
html`<div class="[.created_&]:content-['project-c/new-folder/new-file.html']"></div>`,
)

await fs.expectFileToContain('./project-a/dist/out.css', [
candidate`[.created_&]:content-['project-b/new-file.html']`,
candidate`[.created_&]:content-['project-b/new-folder/new-file.html']`,
candidate`[.created_&]:content-['project-c/new-file.html']`,
candidate`[.created_&]:content-['project-c/new-folder/new-file.html']`,
])
},
)

Expand Down
15 changes: 15 additions & 0 deletions integrations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface TestContext {
getFreePort(): Promise<number>
fs: {
write(filePath: string, content: string): Promise<void>
create(filePaths: string[]): Promise<void>
read(filePath: string): Promise<string>
glob(pattern: string): Promise<[string, string][]>
dumpFiles(pattern: string): Promise<string>
Expand Down Expand Up @@ -294,6 +295,20 @@ export function test(
await fs.mkdir(dir, { recursive: true })
await fs.writeFile(full, content)
},

async create(filenames: string[]): Promise<void> {
for (let filename of filenames) {
let full = path.join(root, filename)

let dir = path.dirname(full)
await fs.mkdir(dir, { recursive: true })

// Create the file _then_ write to it which is necessary because
// postcss-cli doesn't listen for new files just updates to them
await fs.writeFile(full, '')
}
},

async read(filePath: string) {
let content = await fs.readFile(path.resolve(root, filePath), 'utf8')

Expand Down

0 comments on commit 8bd9176

Please sign in to comment.