Skip to content

Commit

Permalink
Fix glob pattern while building the content catalog (asciidoctor#827)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored Nov 25, 2023
1 parent 4254baf commit 164b776
Show file tree
Hide file tree
Showing 35 changed files with 261 additions and 115 deletions.
2 changes: 2 additions & 0 deletions examples/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions src/features/antora/antoraSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ export async function getAntoraDocumentContext (textDocumentUri: Uri, workspaceS
.map(async (antoraConfig) => {
const workspaceFolder = getWorkspaceFolder(antoraConfig.uri)
const workspaceRelative = posixpath.relative(workspaceFolder.uri.path, antoraConfig.contentSourceRootPath)
const files = await Promise.all((await vscode.workspace.findFiles(workspaceRelative + '/modules/*/{attachments,examples,images,pages,partials,assets}/**')).map(async (file) => {
const globPattern = 'modules/*/{attachments,examples,images,pages,partials,assets}/**'
const files = await Promise.all((await vscode.workspace.findFiles(`${workspaceRelative ? `${workspaceRelative}/` : ''}${globPattern}`)).map(async (file) => {
const contentSourceRootPath = antoraConfig.contentSourceRootPath
return {
base: contentSourceRootPath,
Expand All @@ -288,13 +289,12 @@ export async function getAntoraDocumentContext (textDocumentUri: Uri, workspaceS
},
}
}))
const contentAggregate = {
return {
name: antoraConfig.config.name,
version: antoraConfig.config.version,
...antoraConfig.config,
files,
}
return contentAggregate
})))
let classifyContent = await import('@antora/content-classifier')
if ('default' in classifyContent) {
Expand Down
74 changes: 51 additions & 23 deletions src/test/antoraCompletionProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,61 @@ import * as vscode from 'vscode'
import assert from 'assert'
import AntoraCompletionProvider from '../features/antora/antoraCompletionProvider'
import { Position } from 'vscode'
import { getDefaultWorkspaceFolderUri } from '../util/workspace'

let workspaceUri
import { createDirectory, createFile, disableAntoraSupport, enableAntoraSupport, removeFiles } from './workspaceHelper'

suite('Antora CompletionsProvider', () => {
setup(() => {
workspaceUri = getDefaultWorkspaceFolderUri()
const createdFiles: vscode.Uri[] = []
suiteSetup(async () => {
createdFiles.push(await createDirectory('docs'))
await createFile(`name: "api"
version: "1.0"
title: Antora
asciidoc:
attributes:
source-language: asciidoc@
xrefstyle: short@
example-caption: false
`, 'docs', 'api', 'antora.yml')

createdFiles.push(await createFile('', 'help.adoc'))
const asciidocFile = await createFile(`image::images/ocean/waves/seaswell.png[]
image::images/mountain.jpeg[]
link:help.adoc[]
`, 'asciidoctorWebViewConverterTest.adoc')
createdFiles.push(asciidocFile)
})
suiteTeardown(async () => {
await removeFiles(createdFiles)
})
test('Should return completion items', async () => {
const provider = new AntoraCompletionProvider()
const file = await vscode.workspace.openTextDocument(vscode.Uri.joinPath(workspaceUri, 'antora', 'multiComponents', 'api', 'modules', 'auth', 'pages', 'jwt', 'page2.adoc'))
const completionsItems = await provider.provideCompletionItems(file, new Position(3, 1))
assert.deepStrictEqual(completionsItems[0].label, {
description: 'asciidoc@',
label: 'source-language',
})
assert.strictEqual(completionsItems[0].insertText, '{asciidoc@}')
assert.deepStrictEqual(completionsItems[1].label, {
description: 'short@',
label: 'xrefstyle',
})
assert.strictEqual(completionsItems[1].insertText, '{short@}')
assert.deepStrictEqual(completionsItems[2].label, {
description: false,
label: 'example-caption',
try {
const provider = new AntoraCompletionProvider()
const file = await createFile(`= JWT Token
`, 'docs', 'api', 'modules', 'auth', 'pages', 'jwt', 'index.adoc')
const textDocument = await vscode.workspace.openTextDocument(file)
await enableAntoraSupport()
const completionsItems = await provider.provideCompletionItems(textDocument, new Position(2, 1))
assert.deepStrictEqual(completionsItems[0].label, {
description: 'asciidoc@',
label: 'source-language',
})
assert.strictEqual(completionsItems[0].insertText, '{asciidoc@}')
assert.deepStrictEqual(completionsItems[1].label, {
description: 'short@',
label: 'xrefstyle',
})
assert.strictEqual(completionsItems[1].insertText, '{short@}')
assert.deepStrictEqual(completionsItems[2].label, {
description: false,
label: 'example-caption',

})
assert.strictEqual(completionsItems[2].insertText, '{false}')
})
assert.strictEqual(completionsItems[2].insertText, '{false}')
} finally {
await disableAntoraSupport()
}
})
})
179 changes: 130 additions & 49 deletions src/test/antoraSupport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import * as vscode from 'vscode'
import * as assert from 'assert'
import 'mocha'
import { findAntoraConfigFile, getAntoraDocumentContext } from '../features/antora/antoraSupport'
import { createDirectories, createDirectory, createFile, createLink, removeFiles } from './workspaceHelper'
import { createDirectories, createDirectory, createFile, createLink, disableAntoraSupport, enableAntoraSupport, removeFiles } from './workspaceHelper'
import { extensionContext } from './helper'
import { getDefaultWorkspaceFolderUri } from '../util/workspace'

async function testGetAntoraConfig ({
asciidocPathUri,
antoraConfigExpectedUri,
}) {
async function testGetAntoraConfig ({ asciidocPathUri, antoraConfigExpectedUri }) {
const antoraConfigUri = await findAntoraConfigFile(asciidocPathUri)
if (antoraConfigExpectedUri === undefined) {
assert.strictEqual(antoraConfigUri, undefined)
Expand All @@ -25,51 +22,107 @@ async function testGetAntoraConfig ({
}
}

suite('Antora Support', () => {
const workspaceUri = getDefaultWorkspaceFolderUri()
const testCases = [
{
title: 'Should return Antora config for document inside "pages" directory which is inside another directory',
asciidocPathSegments: ['antora', 'multiComponents', 'cli', 'modules', 'commands', 'pages', 'page1.adoc'],
antoraConfigExpectedPathSegments: ['antora', 'multiComponents', 'cli', 'antora.yml'],
},
{
suite('Antora support with multi-documentation components', () => {
const createdFiles = []
const testCases = []
suiteSetup(async () => {
createdFiles.push(await createDirectory('docs'))
// documentation component: docs/multiComponents/api
const apiDocumentationComponentPaths = ['docs', 'multiComponents', 'api']
const apiAntoraPaths = [...apiDocumentationComponentPaths, 'antora.yml']
await createFile(`name: "api"
version: "1.0"
`, ...apiAntoraPaths)
const endpointsPaths = [...apiDocumentationComponentPaths, 'modules', 'auth', 'pages', 'endpoints.adoc']
await createFile('= Endpoints', ...endpointsPaths)
const ssoPaths = [...apiDocumentationComponentPaths, 'modules', 'auth', 'pages', '3rd-party', 'sso.adoc']
await createFile('= Single Sign On', ...ssoPaths)
const tokenBasedPaths = [...apiDocumentationComponentPaths, 'modules', 'auth', 'pages', 'modules', 'token-based.adoc']
await createFile('= Token Based', ...tokenBasedPaths)
const patPaths = [...apiDocumentationComponentPaths, 'modules', 'auth', 'pages', 'modules', 'token', 'pat.adoc']
await createFile('= Personal Access Token', ...patPaths)
//await createFile('= Client Id & Client Secret', ...[...apiDocumentationComponentPaths, 'modules', 'auth', 'pages', 'modules', 'credentials', 'secret.adoc'])
testCases.push({
title: 'Should return Antora config for document inside a "modules" subdirectory',
asciidocPathSegments: tokenBasedPaths,
antoraConfigExpectedPathSegments: apiAntoraPaths,
})
testCases.push({
title: 'Should return Antora config for document inside "pages" directory',
asciidocPathSegments: ['antora', 'multiComponents', 'api', 'modules', 'auth', 'pages', 'page3.adoc'],
antoraConfigExpectedPathSegments: ['antora', 'multiComponents', 'api', 'antora.yml'],
},
{
asciidocPathSegments: endpointsPaths,
antoraConfigExpectedPathSegments: apiAntoraPaths,
})
testCases.push({
title: 'Should return Antora config for document inside a subdirectory',
asciidocPathSegments: ['antora', 'multiComponents', 'api', 'modules', 'auth', 'pages', 'jwt', 'page2.adoc'],
antoraConfigExpectedPathSegments: ['antora', 'multiComponents', 'api', 'antora.yml'],
},
{
title: 'Should return Antora config for document inside a "modules" subdirectory',
asciidocPathSegments: ['antora', 'multiComponents', 'api', 'modules', 'auth', 'pages', 'modules', 'page4.adoc'],
antoraConfigExpectedPathSegments: ['antora', 'multiComponents', 'api', 'antora.yml'],
},
{
title: 'Should return Antora config for document inside a "modules" directory which is inside an Antora modules in a component named "modules"',
asciidocPathSegments: ['antora', 'multiComponents', 'modules', 'api', 'docs', 'modules', 'asciidoc', 'pages', 'modules', 'page5.adoc'],
antoraConfigExpectedPathSegments: ['antora', 'multiComponents', 'modules', 'api', 'docs', 'antora.yml'],
},
{
asciidocPathSegments: ssoPaths,
antoraConfigExpectedPathSegments: apiAntoraPaths,
})
testCases.push({
title: 'Should return Antora config for document inside a directory which has the same name as the workspace',
asciidocPathSegments: ['antora', 'multiComponents', 'api', 'modules', 'auth', 'pages', 'modules', 'multiComponents', 'page6.adoc'],
antoraConfigExpectedPathSegments: ['antora', 'multiComponents', 'api', 'antora.yml'],
},
{
asciidocPathSegments: patPaths,
antoraConfigExpectedPathSegments: apiAntoraPaths,
})

// documentation component: docs/multiComponents/cli
const cliDocumentationComponentPaths = ['docs', 'multiComponents', 'cli']
const cliAntoraPaths = [...cliDocumentationComponentPaths, 'antora.yml']
await createFile(`name: "cli"
version: "2.0"
`, ...cliAntoraPaths)
await createFile('', ...[...cliDocumentationComponentPaths, 'modules', 'commands', 'images', 'output.png'])
const convertPaths = [...cliDocumentationComponentPaths, 'module', 'commands', 'pages', 'convert.adoc']
await createFile(`= Convert Command
image::2.0@cli:commands:output.png[]
image::commands:output.png[]
image::output.png[]
`, ...convertPaths)
testCases.push({
title: 'Should return Antora config for document inside "pages" directory which is inside another directory',
asciidocPathSegments: convertPaths,
antoraConfigExpectedPathSegments: cliAntoraPaths,
})

// documentation component: docs/multiComponents/modules/api/docs/modules
const modulesDocumentationComponentPaths = ['docs', 'multiComponents', 'modules', 'api', 'docs', 'modules']
const modulesAntoraPaths = [...modulesDocumentationComponentPaths, 'antora.yml']
await createFile(`name: asciidoc
version: ~
`, ...modulesAntoraPaths)
const admonitionPagePaths = [...modulesDocumentationComponentPaths, 'blocks', 'pages', 'admonition.adoc']
await createFile(`= Admonition Block
`, ...admonitionPagePaths)
testCases.push({
title: 'Should return Antora config for document inside a "modules" directory which is inside an Antora modules in a component named "modules"',
asciidocPathSegments: admonitionPagePaths,
antoraConfigExpectedPathSegments: modulesAntoraPaths,
})

// outside documentation modules
const writerGuidePaths = ['docs', 'multiComponents', 'api', 'modules', 'writer-guide.adoc']
await createFile('= Writer Guide', ...writerGuidePaths)
testCases.push({
title: 'Should not return Antora config for document outside "modules" Antora folder',
asciidocPathSegments: ['antora', 'multiComponents', 'api', 'modules', 'writer-guide.adoc'],
asciidocPathSegments: writerGuidePaths,
antoraConfigExpectedPathSegments: undefined,
},
{
title: 'Should not return Antora config for document outside of workspace',
asciidocPathSegments: ['antora', 'contributing.adoc'],
})
const contributingPaths = ['docs', 'contributing.adoc']
await createFile('= Contributing', ...contributingPaths)
testCases.push({
title: 'Should not return Antora config for document outside of documentation modules',
asciidocPathSegments: contributingPaths,
antoraConfigExpectedPathSegments: undefined,
},
]
})
})

suiteTeardown(async () => {
await removeFiles(createdFiles)
})

const workspaceUri = getDefaultWorkspaceFolderUri()
for (const testCase of testCases) {
test(testCase.title, async () => testGetAntoraConfig({
asciidocPathUri: vscode.Uri.joinPath(workspaceUri, ...testCase.asciidocPathSegments),
Expand All @@ -92,11 +145,8 @@ suite('Antora Support', () => {
version: '7.1'
`, 'antora-test', 'docs', 'antora.yml')
// enable Antora support
const workspaceConfiguration = vscode.workspace.getConfiguration('asciidoc', null)
await workspaceConfiguration.update('antora.enableAntoraSupport', true)
await enableAntoraSupport()
const workspaceState = extensionContext.workspaceState
await workspaceState.update('antoraSupportSetting', true)
// GO!
const result = await getAntoraDocumentContext(asciidocFile, workspaceState)
const components = result.getComponents()
assert.strictEqual(components !== undefined, true, 'Components must not be undefined')
Expand All @@ -108,9 +158,40 @@ version: '7.1'
throw err
} finally {
await removeFiles(createdFiles)
await extensionContext.workspaceState.update('antoraSupportSetting', undefined)
await vscode.workspace.getConfiguration('asciidoc', null).update('antora.enableAntoraSupport', undefined)
await disableAntoraSupport()
}
}
})
})

suite('Antora support with single documentation component', () => {
test('Should build content catalog', async () => {
const createdFiles = []
try {
createdFiles.push(await createDirectory('modules'))
await createDirectories('modules', 'ROOT', 'pages')
const asciidocFile = await createFile('image:mountain.jpeg[]', 'modules', 'ROOT', 'pages', 'landscape.adoc')
createdFiles.push(asciidocFile)
createdFiles.push(await createFile('', 'modules', 'ROOT', 'images', 'mountain.jpeg'))
createdFiles.push(await createFile(`name: ROOT
version: ~
`, 'antora.yml'))
await enableAntoraSupport()
const workspaceState = extensionContext.workspaceState
const result = await getAntoraDocumentContext(asciidocFile, workspaceState)
const images = result.getImages()
assert.strictEqual(images !== undefined, true, 'Images must not be undefined')
assert.strictEqual(images.length > 0, true, 'Must contains one image')
assert.strictEqual(images[0].src.basename, 'mountain.jpeg')
assert.strictEqual(images[0].src.component, 'ROOT')
assert.strictEqual(images[0].src.family, 'image')
assert.strictEqual(images[0].src.version, null)
} catch (err) {
console.error('Something bad happened!', err)
throw err
} finally {
await removeFiles(createdFiles)
await disableAntoraSupport()
}
})
})
51 changes: 32 additions & 19 deletions src/test/asciidocParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { AsciidoctorExtensions } from '../features/asciidoctorExtensions'
import { AsciidoctorDiagnostic } from '../features/asciidoctorDiagnostic'
import { AsciidoctorExtensionsSecurityPolicyArbiter } from '../security'
import { InMemoryDocument } from './inMemoryDocument'
import { getDefaultWorkspaceFolderUri } from '../util/workspace'
import { createDirectory, createFile, disableAntoraSupport, enableAntoraSupport, removeFiles } from './workspaceHelper'

class TestWebviewResourceProvider implements WebviewResourceProvider {
asWebviewUri (resource: vscode.Uri): vscode.Uri {
Expand Down Expand Up @@ -48,24 +48,37 @@ class AsciidocContributionProviderTest implements AsciidocContributionProvider {

suite('AsciiDoc parser with Antora support enabled', function () {
this.timeout(60000)
const workspaceUri = getDefaultWorkspaceFolderUri()
test('convert Antora page', async () => {
await extensionContext.workspaceState.update('antoraSupportSetting', true)
await vscode.workspace.getConfiguration('asciidoc', null).update('antora.enableAntoraSupport', true)
const asciidocParser = new AsciidocEngine(
new AsciidocContributionProviderTest(extensionContext.extensionUri),
new AsciidoctorConfig(),
new AsciidoctorExtensions(AsciidoctorExtensionsSecurityPolicyArbiter.activate(extensionContext)),
new AsciidoctorDiagnostic('test')
)
const result = await asciidocParser.convertFromTextDocument(
new InMemoryDocument(
vscode.Uri.file(`${workspaceUri.path}/antora/multiComponents/api/modules/auth/pages/page.adoc`),
'Download from the {url-vscode-marketplace}[Visual Studio Code Marketplace].'
),
extensionContext,
new TestWebviewResourceProvider()
)
assert.strictEqual(result.html.includes('<p>Download from the <a href="https://marketplace.visualstudio.com/vscode" data-href="https://marketplace.visualstudio.com/vscode">Visual Studio Code Marketplace</a>.</p>'), true)
const createdFiles = []
try {
createdFiles.push(await createDirectory('docs'))
await createFile(`name: "antora"
version: "1.1.1"
title: Antora
asciidoc:
attributes:
url-vscode-marketplace: https://marketplace.visualstudio.com/vscode
`, 'docs', 'antora.yml')
const asciidocFile = await createFile('', 'docs', 'modules', 'ROOT', 'pages', 'index.adoc') // virtual
await enableAntoraSupport()
const asciidocParser = new AsciidocEngine(
new AsciidocContributionProviderTest(extensionContext.extensionUri),
new AsciidoctorConfig(),
new AsciidoctorExtensions(AsciidoctorExtensionsSecurityPolicyArbiter.activate(extensionContext)),
new AsciidoctorDiagnostic('test')
)
const result = await asciidocParser.convertFromTextDocument(
new InMemoryDocument(
asciidocFile,
'Download from the {url-vscode-marketplace}[Visual Studio Code Marketplace].'
),
extensionContext,
new TestWebviewResourceProvider()
)
assert.strictEqual(result.html.includes('<p>Download from the <a href="https://marketplace.visualstudio.com/vscode" data-href="https://marketplace.visualstudio.com/vscode">Visual Studio Code Marketplace</a>.</p>'), true)
} finally {
await removeFiles(createdFiles)
await disableAntoraSupport()
}
})
})
Loading

0 comments on commit 164b776

Please sign in to comment.