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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ jobs:
# `index.node`, which nothing else in this job produces.
#
# NOT for protect-ffi's own suite, though vendoring it did add
# `@cipherstash/protect-ffi#test` to `turbo test --filter './languages/typescript/packages/*'`.
# `@cipherstash/protect-ffi#test` to `turbo test --filter './languages/typescript/packages/**' --filter './packages/**'`.
# That suite must pass with no binding at all (its `build` is `tsc` and
# its `test` is deliberately cargo-free), and `src/lintWiring.test.ts`
# re-runs it here with the artifact hidden to keep that true — this job
Expand Down
6 changes: 4 additions & 2 deletions languages/typescript/packages/protect-ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ Builds the `wasm32-unknown-unknown` output into `dist/wasm/` with `wasm-pack`, t

#### `pnpm test`

Typechecks the TypeScript, runs the unit tests (tsc + vitest), and lints and format-checks the TypeScript. **No cargo**, by design — see above.
Typechecks the TypeScript, runs the unit tests (vitest, against `lib/` from `build`), and lints and format-checks the TypeScript. **No cargo**, by design — see above.

The unit tests read `lib/`, and `pnpm test` does not write it: its typecheck runs `tsc --noEmit`, because other packages' builds read `lib/` while root `pnpm test` runs. Run `pnpm run build` first. Through turbo (root `pnpm test`), `build` runs before `test` on its own.
Comment thread
auxesis marked this conversation as resolved.
Resolved

Note: this does not run the integration tests either.
For those, see [below](#integration-tests).
Expand All @@ -265,7 +267,7 @@ The Rust half of the test suite: `cargo test --locked` plus `cargo fmt --check`.

#### `pnpm run test:typecheck:wasm`

Typechecks `type-tests/` against the wasm-bindgen-generated declarations in `dist/wasm/`. Needs `build:wasm` and `build` to have run first, which is why it is not part of `pnpm test` — that has to pass in a fresh clone with no `dist/`.
Typechecks `type-tests/` against the wasm-bindgen-generated declarations in `dist/wasm/`. Needs `build:wasm` and `build` to have run first. Root `pnpm test` has to pass in a fresh clone with no `dist/`, so `pnpm test` leaves this out.

#### `mise run lint:rust`

Expand Down
2 changes: 1 addition & 1 deletion languages/typescript/packages/protect-ffi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"scripts": {
"test": "pnpm run test:typecheck && pnpm run test:unit && pnpm run test:lint && pnpm run test:format:ts",
"test:cargo": "pnpm run test:rust && pnpm run test:format:rust",
"test:typecheck": "tsc && tsc -p tsconfig.test.json",
"test:typecheck": "tsc --noEmit && tsc -p tsconfig.test.json",
Comment thread
auxesis marked this conversation as resolved.
Resolved
"test:typecheck:wasm": "tsc -p type-tests/tsconfig.json",
"test:unit": "vitest run",
"test:rust": "cargo test --locked",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ describe('lint and format wiring', () => {

it('keeps cargo off the default test path', () => {
// The load-bearing half of the split. Root `pnpm test` runs
// `turbo test --filter './languages/typescript/packages/*'`, which reaches this package's
// `turbo test --filter './languages/typescript/packages/**' --filter './packages/**'`, which reaches this package's
// `test` — so anything cargo on that path is cargo on every PR, in a repo
// where one package out of eighteen is Rust.
//
Expand Down Expand Up @@ -424,7 +424,7 @@ describe('lint and format wiring', () => {
// test then requires what cargo produces: `src/nativeLoading.test.ts`
// asserted `assertNativeBindingAvailable()` does not throw, which needs
// an `index.node` that only `build:native` writes. Root `pnpm test`
// reaches this package through `turbo test --filter './languages/typescript/packages/*'`, so
// reaches this package through `turbo test --filter './languages/typescript/packages/**' --filter './packages/**'`, so
// that made a Rust build a prerequisite of the whole repo's default test
// — the exact thing the entry-point split exists to prevent, arriving
// through the tests instead of through the scripts.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
* A regression here is silent for anyone with a binary installed — every
* consumer who installs from npm, and every CI job that has run
* `.github/actions/build-ffi-binding` — so the property is asserted against the
* EMITTED JavaScript rather than behaviour. `lib/` exists by the time this
* runs: `test:typecheck` emits it before `test:unit`.
* EMITTED JavaScript rather than behaviour. `lib/` comes from `build`, and
* turbo runs this package's `build` before its `test` (`turbo.json`,
* `@cipherstash/protect-ffi#test`). `test` itself never writes `lib/`:
* `test:typecheck` runs `tsc --noEmit`, because other packages' builds read
* `lib/` while this suite runs.
*
* The same blind spot is why the last block goes the other way and asserts
* behaviour: what `assertNativeBindingAvailable` does with a MISSING binary
Expand All @@ -24,29 +27,41 @@
*
* Nothing in this file may REQUIRE a binary, though, and that is a separate
* rule from the ones above. `languages/typescript/packages/protect-ffi`'s `test` is the default task
* root `pnpm test` reaches through `turbo test --filter './languages/typescript/packages/*'`, and it
* root `pnpm test` reaches through `turbo test --filter './languages/typescript/packages/**' --filter './packages/**'`, and it
* is deliberately Rust-free: `index.node` stopped being tarball content when
* this package was absorbed, so on a fresh checkout there is no binary
* anywhere and the six `platforms/*` links are empty. `lintWiring.test.ts`
* guards that rule for the whole suite by re-running it with the artifact made
* unresolvable.
*/

import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
import {
existsSync,
mkdirSync,
mkdtempSync,
readdirSync,
readFileSync,
rmSync,
statSync,
utimesSync,
writeFileSync,
} from 'node:fs'
import Module, { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'

// Vitest resolves cwd to the directory holding vitest.config.ts.
const packageRoot = process.cwd()
const entryPath = join(packageRoot, 'lib/index.cjs')

// `lib/` is generated, so this file carries a prerequisite the package's `test`
// chain satisfies and a bare `test:unit` does not. Without this guard the whole
// `lib/` is generated, so this file carries a prerequisite that turbo's
// `test` satisfies (it runs `build` first) and a bare `pnpm run test` or
// `test:unit` does not. Without this guard the whole
// suite fails as `ENOENT: no such file or directory, open '.../lib/index.cjs'`
// pointed at the readFileSync below — which names the missing file but not the
// command that produces it, and lands on whoever runs `test:unit` directly
// (every CI job that splits the chain, and anyone iterating on one test).
// command that produces it, and lands on whoever runs the package script
// directly (anyone iterating on one test).
// A zero-byte entry counts as missing. An interrupted `tsc` leaves one, and it
// is the state where naming the build step — the entire point of this guard —
// otherwise fails to happen: `existsSync` is satisfied, and what the developer
Expand All @@ -55,12 +70,36 @@ const entryPath = join(packageRoot, 'lib/index.cjs')
// source` below already fails loudly on an empty read, which is why it is
// there. This is so the failure names its own cure.
//
// Deliberately only the empty case. A PARTIALLY written entry is not
// Only the empty case for size. A PARTIALLY written entry is not
// detectable by size and needs no separate handling — the content assertions
// below go red on any degraded emit; they cannot go quietly green.
if (!existsSync(entryPath) || statSync(entryPath).size === 0) {
throw new Error(
`${entryPath} is missing or empty. These tests assert on the EMITTED entry, and lib/ is a build output: run \`pnpm --filter @cipherstash/protect-ffi run build\` first, or \`pnpm --filter @cipherstash/protect-ffi test\`, whose test:typecheck step emits lib/ before test:unit runs.`,
`${entryPath} is missing or empty. These tests assert on the EMITTED entry, and lib/ is a build output: run \`pnpm --filter @cipherstash/protect-ffi run build\` first, or \`pnpm turbo run test --filter @cipherstash/protect-ffi\`, which runs build before test. The package's own test script does not write lib/.`,
)
}

/**
* The sources under `srcDir` that tsc compiles into `lib/` and that changed
* after `entry` was written, as paths relative to `srcDir`. Test files are
* left out, because the build tsconfig excludes them.
*/
function sourcesNewerThan(entry: string, srcDir: string): string[] {
const builtAt = statSync(entry).mtimeMs
return readdirSync(srcDir, { recursive: true, encoding: 'utf8' })
.filter((file) => /\.[cm]?ts$/.test(file) && !file.endsWith('.test.ts'))
.filter((file) => statSync(join(srcDir, file)).mtimeMs > builtAt)
.sort()
}

// A stale entry fails the same way. The package's `test` no longer writes
// lib/, so after a source edit a bare `test` would otherwise assert on the
// JavaScript the previous build emitted, and pass. CI is not affected: every
// job builds lib/ after checkout, so lib/ is newer than every source.
const staleSources = sourcesNewerThan(entryPath, join(packageRoot, 'src'))
if (staleSources.length > 0) {
throw new Error(
`${entryPath} is older than ${staleSources.join(', ')}. These tests assert on the EMITTED entry, so they would test the previous build: run \`pnpm --filter @cipherstash/protect-ffi run build\` first.`,
)
}

Expand Down Expand Up @@ -196,7 +235,7 @@ describe('assertNativeBindingAvailable', () => {
// empty until someone builds one, and `languages/typescript/packages/protect-ffi`'s `test` is
// deliberately Rust-free. So on every fresh checkout the assertion failed
// — and it failed under root `pnpm test`, which reaches this package via
// `turbo test --filter './languages/typescript/packages/*'`.
// `turbo test --filter './languages/typescript/packages/**' --filter './packages/**'`.
//
// Both branches assert. A `skipIf` here would be the trade this repo keeps
// refusing: it goes quiet on every contributor machine, and quiet is
Expand Down Expand Up @@ -514,3 +553,30 @@ describe('assertNativeBindingAvailable with the platform binary missing', () =>
expect((caught as NodeJS.ErrnoException).code).toBe('MODULE_NOT_FOUND')
})
})

describe('sourcesNewerThan', () => {
// The guard at the top of this file uses it to reject a stale lib/. It runs
// on a temporary tree here, because the real tree's mtimes depend on when
// build last ran.
it('lists compiled sources newer than the entry, and nothing else', () => {
const root = mkdtempSync(join(tmpdir(), 'protect-ffi-stale-'))
try {
const src = join(root, 'src')
mkdirSync(join(src, 'nested'), { recursive: true })
const writeAt = (path: string, seconds: number) => {
writeFileSync(path, '')
utimesSync(path, seconds, seconds)
}
const entry = join(root, 'index.cjs')
writeAt(entry, 2000)
writeAt(join(src, 'old.cts'), 1000)
writeAt(join(src, 'nested', 'newer.ts'), 3000)
writeAt(join(src, 'newer.test.ts'), 3000)
writeAt(join(src, 'notes.md'), 3000)

expect(sourcesNewerThan(entry, src)).toEqual([join('nested', 'newer.ts')])
} finally {
rmSync(root, { recursive: true, force: true })
}
})
})
Loading
Loading