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
16 changes: 12 additions & 4 deletions packages/api-model/src/languages/jvm-examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export function attachCompleteJvmExamples(
}
ids.add(program.id)
const files = manifest.variants[program.variant]?.files
if (!Array.isArray(files) || files.length !== 4 || new Set(files.map((file) => file.path)).size !== 4 ||
// A revision before libtmux-java#142 ships run.sh as a fourth setup file; later ones start their own server.
const launcher = Array.isArray(files) && files.some((file) => file.path === 'run.sh')
const setupCount = launcher ? 4 : 3
if (!Array.isArray(files) || files.length !== setupCount ||
new Set(files.map((file) => file.path)).size !== setupCount ||
!files.every((file) => Object.hasOwn(setupLanguages, file.path))) {
throw new Error(`Invalid JVM example setup: ${program.variant}`)
}
Expand All @@ -120,12 +124,16 @@ export function attachCompleteJvmExamples(
})
blocks.push({
lang: port,
intro: `Save this complete program as \`${program.path}\`. It opens an ordinary client for the launcher's server; closing the client releases its transport.`,
intro: launcher
? `Save this complete program as \`${program.path}\`. It opens an ordinary client for the launcher's server; closing the client releases its transport.`
: `Save this complete program as \`${program.path}\`. It starts a private tmux server of its own and stops it when the program closes it.`,
sourceUrl: urlFor(program.sourceFile), code: codeFor(program.sourceFile),
}, {
lang: 'console',
intro: 'Build the minimal consumer with Gradle and run the saved program with the private fixture. An operation or cleanup error makes the command exit unsuccessfully:',
code: `$ gradle --console=plain --quiet installDist -PexampleMain=${program.mainClass} && \\\n sh run.sh build/install/api-example/bin/api-example\n`,
intro: launcher
? 'Build the minimal consumer with Gradle and run the saved program with the private fixture. An operation or cleanup error makes the command exit unsuccessfully:'
: 'Build the minimal consumer with Gradle and run the saved program. An operation or cleanup error makes the command exit unsuccessfully:',
code: `$ gradle --console=plain --quiet installDist -PexampleMain=${program.mainClass}${' && \\\n '}${launcher ? 'sh run.sh ' : ''}build/install/api-example/bin/api-example\n`,
}, { lang: 'text', intro: 'Expected output:', code: program.expectedOutput })
const targets = new Set<string>()
for (const target of program.targets) {
Expand Down
40 changes: 35 additions & 5 deletions packages/api-model/test/jvm-examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ApiSymbol } from '../src/model.ts'

const source = { repo: 'libtmux/libtmux-java', revision: 'a'.repeat(40), releaseTag: 'v0.0.1-alpha.18' }
const variants = ['java', 'kotlin', 'scala-direct', 'scala-cats'] as const
const fixture = () => {
const fixture = (launcher = true) => {
const files = new Map<string, string>()
const examples = variants.map((variant) => {
const language = variant.startsWith('scala') ? 'scala' : variant as 'java' | 'kotlin'
Expand All @@ -19,7 +19,7 @@ const fixture = () => {
const manifest = {
schemaVersion: 1, examples,
variants: Object.fromEntries(variants.map((variant) => [variant, { files:
['settings.gradle.kts', 'build.gradle.kts', 'gradle.properties', 'run.sh'].map((path) => {
['settings.gradle.kts', 'build.gradle.kts', 'gradle.properties', ...(launcher ? ['run.sh'] : [])].map((path) => {
const sourceFile = `examples/api/${variant}/${path}`
files.set(sourceFile, path === 'settings.gradle.kts'
? 'rootProject.name = "api-example"\n\nincludeBuild("libtmux-source")\n'
Expand Down Expand Up @@ -74,6 +74,34 @@ describe('complete JVM API programs', () => {
}
})

it.each(['java', 'kotlin', 'scala'] as const)('shows %s programs that start their own server without a launcher', (port) => {
const { manifest, symbols, files, read } = fixture(false)
attachCompleteJvmExamples(symbols, port, read, source)
for (const [i, program] of manifest.examples.entries()) {
const blocks = symbols[i].doc?.examples
if (program.language !== port) { expect(blocks).toBeUndefined(); continue }
expect(blocks).toHaveLength(6)
const [, , properties, code, command, output] = blocks!
expect(manifest.variants[program.variant].files.map((file) => file.path))
.toEqual(['settings.gradle.kts', 'build.gradle.kts', 'gradle.properties'])
expect(properties.code).toBe(files.get(manifest.variants[program.variant].files[2].sourceFile))
expect(code.lang).toBe(port)
expect(code.code).toBe(files.get(program.sourceFile))
expect(command.code).toBe(`$ gradle --console=plain --quiet installDist -PexampleMain=${program.mainClass} && \\\n build/install/api-example/bin/api-example\n`)
expect(output).toEqual({ lang: 'text', intro: 'Expected output:', code: program.expectedOutput })
const prose = blocks!.map((block) => `${block.intro ?? ''}\n${block.code}`).join('\n')
expect(prose).not.toMatch(/run\.sh|launcher|fixture/)
}
})

it('still says launcher where the revision ships one', () => {
const { symbols, read } = fixture()
attachCompleteJvmExamples(symbols, 'java', read, source)
const blocks = symbols[0].doc!.examples!
expect(blocks[4].intro).toContain("launcher's server")
expect(blocks[5].code).toContain('sh run.sh build/install/api-example/bin/api-example')
})

it('keeps older source revisions without a manifest unchanged', () => {
const { symbols } = fixture()
const before = structuredClone(symbols)
Expand Down Expand Up @@ -118,9 +146,11 @@ describe('complete JVM API programs', () => {
})

it('rejects incomplete setup, missing source bytes and unsafe commands or paths', () => {
const setup = fixture()
setup.manifest.variants.java.files.pop()
expect(() => attachCompleteJvmExamples(setup.symbols, 'java', setup.read, source)).toThrow('Invalid JVM example setup')
for (const launcher of [true, false]) {
const setup = fixture(launcher)
setup.manifest.variants.java.files.shift()
expect(() => attachCompleteJvmExamples(setup.symbols, 'java', setup.read, source)).toThrow('Invalid JVM example setup')
}
const missing = fixture()
missing.files.delete(missing.manifest.examples[0].sourceFile)
expect(() => attachCompleteJvmExamples(missing.symbols, 'java', missing.read, source)).toThrow('Missing complete JVM example file')
Expand Down
10 changes: 6 additions & 4 deletions site/test/jvm-api-examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ describe('verified complete Java-family API programs', () => {
const exported = fromMarkdown(markdown).children.filter((node) => node.type === 'code')
expect(symbol.source.revision).toBe(example.sourceRevision)
expect(example.page).toBe(`ports/${example.port}/reference/${symbol.slug}`)
expect(example.files).toHaveLength(5)
// Four setup files and the program until libtmux-java#142 removed run.sh, three after it.
expect([4, 5]).toContain(example.files.length)
const programFile = example.files.length - 1
for (const file of example.files) {
// settings and build are the port's files rewritten for the Maven Central release, so they cite no source.
const rewritten = file.name === 'settings.gradle.kts' || file.name === 'build.gradle.kts'
Expand Down Expand Up @@ -74,11 +76,11 @@ describe('verified complete Java-family API programs', () => {
expect(blocks[index].lang).toBe('console')
expect(exported.some((entry) => entry.value + '\n' === blocks[index].code)).toBe(true)
}
const program = blocks[example.files[4].block]
const program = blocks[example.files[programFile].block]
expect(program.lang).toBe(example.port)
if (example.sourceProgramId.startsWith('scala-cats-')) expect(program.code).toContain('extends IOApp')
if (example.sourceProgramId.startsWith('scala-direct-')) expect(program.code).toContain('Using.resource')
if (example.port === 'java') expect(program.code).toContain('try (Server server = Server.open(')
if (example.port === 'kotlin') expect(program.code).toContain('withServer(')
if (example.port === 'java') expect(program.code).toMatch(/try \(Server server = Server\.(?:open|start)\(/)
if (example.port === 'kotlin') expect(program.code).toMatch(/with(?:Private)?Server\b/)
})
})
Loading