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
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import assert from 'node:assert/strict'
import { realpathSync } from 'node:fs'
import { basename, join, relative, resolve } from 'node:path'

import type { BunPlugin, OnLoadCallback, PluginBuilder } from 'bun'

const [fixture, schedule] = process.argv.slice(2)
assert(fixture && (schedule === 'AB' || schedule === 'BA'))
process.chdir(fixture)
// The parent spawns this process inside the fixture; the plugin resolves df
// from that cwd at import time, so never change directory here.
assert.equal(realpathSync.native(process.cwd()), realpathSync.native(fixture))

// Import only after entering the isolated fixture: the plugin resolves df at import time.
const { DevupUI } = await import('../plugin')
const { getUnreservedClassSlotCount } = await import('@devup-ui/wasm')
const a = join(fixture, 'src/A.tsx')
Expand Down
9 changes: 2 additions & 7 deletions packages/next-plugin/src/__tests__/atom-hoist-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import assert from 'node:assert/strict';
import { mkdirSync, writeFileSync, readFileSync, rmSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
const root = ${JSON.stringify(root)};
const original = process.cwd();
const sources = {
'src/shared.tsx': 'import { Box } from "@devup-ui/react"; export const Shared = () => <Box color="red" p={4} />;',
'src/app/a/page.tsx': 'import { Box } from "@devup-ui/react"; import { Shared } from "../../shared"; export default function A() { return <><Shared /><Box opacity={0.25} /></>; }',
Expand All @@ -46,8 +45,6 @@ mkdirSync(cssDir, { recursive: true });
writeFileSync(join(root, 'devup.json'), '{}');
writeFileSync(join(root, 'tsconfig.json'), '{}');
writeFileSync(join(root, 'package.json'), '{"name":"atom-hoist-fixture","private":true}');
process.chdir(root);
try {
${setup}
const outputs = {};
for (const file of ${reverse} ? [...files].reverse() : files) {
Expand Down Expand Up @@ -115,13 +112,11 @@ ${setup}
sheets: Object.fromEntries(Object.entries(sheets).sort().map(([name, css]) => [name, clean(css)])),
}));
await finish();
} finally {
process.chdir(original);
}
process.exit(0);
`,
],
{ encoding: 'utf8', timeout: 30_000, env: process.env },
// The fixture is the cwd from the start, as for a real build.
{ cwd: root, encoding: 'utf8', timeout: 30_000, env: process.env },
).trim()
try {
return [run(false), run(true), run(false)] as const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ const schemes: Record<string, (root: string) => (path: string) => string> = {
}

describe('class and file numbers do not depend on the order files are seen', () => {
// Every path below is built from root; the process cwd is never changed.
let root: string
const original = process.cwd()

beforeAll(() => {
root = mkdtempSync(join(tmpdir(), 'devup-ui-numbering-order-'))
for (const [path, code] of Object.entries(sources)) {
mkdirSync(dirname(join(root, path)), { recursive: true })
writeFileSync(join(root, path), code)
}
process.chdir(root)
})

afterAll(() => {
wasm.resetBuildState()
process.chdir(original)
rmSync(root, { recursive: true, force: true })
})

Expand Down
11 changes: 6 additions & 5 deletions packages/next-plugin/src/__tests__/imported-styled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import { dirname, join } from 'node:path'
import { pathToFileURL } from 'node:url'

import * as wasm from '@devup-ui/wasm'
import { afterEach, beforeEach, describe, expect, it } from 'bun:test'
import { afterEach, beforeEach, describe, expect, it, spyOn } from 'bun:test'

import { withModuleResolver } from '../wasm'
import { assertImportedStyledRendering } from './imported-styled-render'

const originalCwd = process.cwd()

describe('styled components another module defines', () => {
let root: string
let cwd: ReturnType<typeof spyOn>

const write = (path: string, code: string) => {
mkdirSync(dirname(join(root, path)), { recursive: true })
Expand Down Expand Up @@ -58,12 +57,14 @@ describe('styled components another module defines', () => {
'node_modules/@acme/ui/src/index.js',
"import styled from '@emotion/styled'\nexport const Card = styled.section`border: 13px solid;`\n",
)
process.chdir(root)
// The Next resolver names files relative to the project cwd; point it at
// the fixture without changing the directory of the whole test process.
cwd = spyOn(process, 'cwd').mockReturnValue(root)
withModuleResolver(wasm)
})

afterEach(() => {
process.chdir(originalCwd)
cwd.mockRestore()
rmSync(root, { recursive: true, force: true })
})

Expand Down
14 changes: 9 additions & 5 deletions packages/next-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { spawnSync } from 'node:child_process'
import * as fs from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'

import type { StaticImportGraph } from '@devup-ui/plugin-utils'
Expand Down Expand Up @@ -254,14 +255,16 @@ describe('DevupUINextPlugin', () => {
})

it('prewarms and serves actual default Turbo extraction through native state', () => {
// The child starts inside the fixture, as Next does, instead of changing
// its directory after the plugin modules have loaded.
const root = fs.mkdtempSync(join(tmpdir(), 'devup-next-native-'))
const child = spawnSync(
process.execPath,
[
'-e',
`
import assert from 'node:assert/strict';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { mkdirSync, realpathSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { DevupUI } from ${JSON.stringify(new URL('../plugin.ts', import.meta.url).href)};
import loader from ${JSON.stringify(new URL('../loader.ts', import.meta.url).href)};
Expand All @@ -270,15 +273,15 @@ describe('DevupUINextPlugin', () => {
import { loadCompiler } from ${JSON.stringify(new URL('../../../plugin-utils/src/index.ts', import.meta.url).href)};
const native = loadCompiler('native'), wasm = loadCompiler('wasm');
const primedWasm = loadWasm('wasm'); // Resolve the old loader dependency at its installed location.
const cwd = process.cwd(), root = mkdtempSync(join(tmpdir(), 'devup-next-native-'));
const root = ${JSON.stringify(root)};
assert.equal(realpathSync.native(process.cwd()), realpathSync.native(root));
try {
mkdirSync(join(root, 'src/app'), { recursive: true });
writeFileSync(join(root, 'tsconfig.json'), JSON.stringify({ compilerOptions: { jsx: 'react-jsx' }, include: ['src'] }));
writeFileSync(join(root, 'src/tokens.ts'), "export const tone = 'teal';");
const source = "import {Box} from '@devup-ui/react'; import {tone} from '../tokens'; export default function Page(){return <Box color={tone}/>;}";
const resourcePath = join(root, 'src/app/page.tsx');
writeFileSync(resourcePath, source);
process.chdir(root);
native.resetStateForTesting(); wasm.resetStateForTesting();
withModuleResolver(primedWasm); // Match normal fixed-cwd supplier resolution after fixture setup.
const before = { native: native.getCss(null, false), wasm: wasm.getCss(null, false) };
Expand All @@ -297,17 +300,18 @@ describe('DevupUINextPlugin', () => {
} finally {
await flushCoordinatorWrites(); resetCoordinator();
native.resetStateForTesting(); wasm.resetStateForTesting();
process.chdir(cwd); rmSync(root, { recursive: true, force: true });
}
`,
],
{
cwd: root,
encoding: 'utf8',
timeout: 12000,
env: { ...process.env, NODE_ENV: 'production', TURBOPACK: '1' },
windowsHide: true,
},
)
fs.rmSync(root, { recursive: true, force: true })
console.info(child.stdout)
expect(child.error).toBeUndefined()
expect(child.signal).toBeNull()
Expand Down
5 changes: 3 additions & 2 deletions packages/vite-plugin/src/__tests__/css-http-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { createServer } from 'vite'

import { DevupUI } from '../plugin'

// This helper is bundled into an owned temp directory, then run only by Node.
// This helper is bundled into an owned temp directory, then run only by Node
// with the project below as its cwd, so it never changes directory itself.
const root = join(
realpathSync(dirname(fileURLToPath(import.meta.url))),
'project',
)
const accept = process.argv[2]
assert(accept === 'text/css' || accept === '*/*')
assert.equal(realpathSync.native(process.cwd()), realpathSync.native(root))
mkdirSync(join(root, 'src'), { recursive: true })
writeFileSync(join(root, 'devup.json'), '{}')
writeFileSync(join(root, 'package.json'), '{"type":"module"}')
Expand All @@ -25,7 +27,6 @@ writeFileSync(
join(root, 'src/main.js'),
"import { globalCss } from '@devup-ui/react'; globalCss({ body: { outlineStyle: 'dashed' } });",
)
process.chdir(root)

// Given: actual extraction and Vite with its default strict filesystem policy.
const server = await createServer({
Expand Down
15 changes: 12 additions & 3 deletions packages/vite-plugin/src/__tests__/css-http-representation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import { mkdtempSync, realpathSync, rmSync, symlinkSync } from 'node:fs'
import {
mkdirSync,
mkdtempSync,
realpathSync,
rmSync,
symlinkSync,
} from 'node:fs'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'

Expand Down Expand Up @@ -36,11 +42,14 @@ it.each(['text/css', '*/*'])(
})
assert(bundle.success, bundle.logs.map(String).join('\n'))

// When: native Node owns Vite and HTTP, avoiding Bun's Windows fs identity.
// When: native Node owns Vite and HTTP, avoiding Bun's Windows fs identity,
// and starts inside the project the probe serves.
const project = join(root, 'project')
mkdirSync(project)
const result = spawnSync(
'node',
[owner, '45000', join(root, 'probe.mjs'), accept],
{ encoding: 'utf8', timeout: 60000, windowsHide: true },
{ cwd: project, encoding: 'utf8', timeout: 60000, windowsHide: true },
)

// Then: all real HTTP assertions passed and the owned process tree drained.
Expand Down
Loading