Skip to content

Commit

Permalink
use production build for fixture tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmca-glasgow committed May 4, 2022
1 parent 243d89f commit 2a10935
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 50 deletions.
1 change: 1 addition & 0 deletions compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"@mapbox/remark-lint-link-text": "^0.6.0",
"base64-arraybuffer": "^1.0.2",
"chalk": "^5.0.0",
"dargs": "^8.1.0",
"dictionary-en-gb": "^2.4.0",
"escape-latex": "^1.2.0",
"figures": "^4.0.0",
Expand Down
17 changes: 16 additions & 1 deletion compiler/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ const { argv } = yargs(process.argv.slice(2))
.option('verbose', {
type: 'boolean',
description: 'Show error stack',
})
.option('output', {
type: 'string',
description: 'output to stdout',
choices: ['md', 'html'],
});

const dirPath = String(argv._[0] || '.');
Expand All @@ -87,11 +92,21 @@ const options: Options = {
pythonBin: argv.pythonBin,
force: argv.force,
verbose: argv.verbose,
output: argv.output as 'md' | 'html',
};

async function run() {
try {
await rMarkdown(dirPath, options);
const weeks = await rMarkdown(dirPath, options);
if (weeks.length === 1) {
const result = weeks[0];
if (options.output === 'html') {
console.log(result.html?.html || '');
}
if (options.output === 'md') {
console.log(result.md);
}
}
} catch (err: any) {
console.log(chalk.red(figures.cross + ' ' + err.message));
if (options.verbose) {
Expand Down
1 change: 1 addition & 0 deletions compiler/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type Options = {
format?: boolean;
pythonBin?: string;
verbose?: boolean;
output?: 'md' | 'html';
};

export type Context = {
Expand Down
14 changes: 11 additions & 3 deletions compiler/src/course/__test__/load-course.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@ import path from 'path';
import { fixtureTestProcessor } from '../../test-utils/fixture-test-processor';

describe('loadCourse', () => {
it('should output a course', async () => {
const md = await fixtureTestProcessor('basic', { output: 'md' });
expect(md).toContain('We have already used this');
});

it('should error when trying to process week that does not exist', async () => {
const courseYaml = path.join('fixtures', 'basic', 'course.yaml');
await expect(
fixtureTestProcessor('basic', { week: 2 })
).rejects.toThrow(`Week 2 not found in ${courseYaml}`);

try {
await fixtureTestProcessor('basic', { week: 2 });
} catch (err) {
expect(err).toContain(`Week 2 not found in ${courseYaml}`);
}
});

it('should not error when generating a pdf', async () => {
Expand Down
17 changes: 12 additions & 5 deletions compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { Timer, createTimer } from './utils/timer';
import { mkdir, writeFile } from './utils/utils';

export async function rMarkdown(dirPath: string, options: Options = {}) {
await checkForLatestVersion();
if (!options.output) {
await checkForLatestVersion();
}

const timer = createTimer();
const ctx = await createContext(dirPath, options);
Expand Down Expand Up @@ -53,8 +55,11 @@ async function writeUnit(built: BuiltUnit, ctx: Context, timer: Timer) {

if (built.html) {
await writeFile(filePath + '.html', built.html.html);
const status = chalk.green.bold(`Complete in ${timer.seconds()}s`);
console.log(`✨ ${status} ${filePath}.html`);

if (!ctx.options.output) {
const status = chalk.green.bold(`Complete in ${timer.seconds()}s`);
console.log(`✨ ${status} ${filePath}.html`);
}
}

if (built.pdf) {
Expand All @@ -63,7 +68,9 @@ async function writeUnit(built: BuiltUnit, ctx: Context, timer: Timer) {
// debug
// await writeFile(filePath + '.pdf.html', built.pdf.html);

const status = chalk.green.bold(`Complete in ${timer.seconds()}s`);
console.log(`✨ ${status} ${filePath}.pdf`);
if (!ctx.options.output) {
const status = chalk.green.bold(`Complete in ${timer.seconds()}s`);
console.log(`✨ ${status} ${filePath}.pdf`);
}
}
}
4 changes: 3 additions & 1 deletion compiler/src/knitr/__test__/multifile-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { fixtureTestProcessor } from '../../test-utils/fixture-test-processor';

describe('multifile knitr state', () => {
it('should allow knitr state to pass between files', async () => {
const { html } = await fixtureTestProcessor('multifile-knitr-state');
const html = await fixtureTestProcessor('multifile-knitr-state', {
output: 'html',
});
expect(html).toContain('<code>[1] 3</code>');
expect(html.replace(/[\\/]+/g, ' ')).toContain(
'test 1 university-of-glasgow.png'
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/mdast/__test__/boxouts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,9 @@ describe('weblink', () => {
});

it('should increment counter across .Rmd files', async () => {
const { html } = await fixtureTestProcessor('multifile-counter');
const html = await fixtureTestProcessor('multifile-counter', {
output: 'html',
});
expect(html.includes('id="task-2"')).toBe(true);
});

Expand Down
3 changes: 2 additions & 1 deletion compiler/src/mdast/__test__/embed-asset-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { fixtureTestProcessor } from '../../test-utils/fixture-test-processor';

describe('embedAssetUrl', () => {
it('should embed asset URL for knitr graphics', async () => {
const { html } = await fixtureTestProcessor('relative-assets', {
const html = await fixtureTestProcessor('relative-assets', {
noEmbedAssets: false,
output: 'html',
});
// console.log(html);
const imgCount = (html.match(/img-wrapper/g) || []).length;
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/mdast/__test__/interactive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { fixtureTestProcessor } from '../../test-utils/fixture-test-processor';

describe('interactive elements', () => {
it('should display embedded .html', async () => {
const { html } = await fixtureTestProcessor('interactive-elements', {
const html = await fixtureTestProcessor('interactive-elements', {
noEmbedAssets: false,
output: 'html',
});

expect(html).toContain('<div id="multivariate-normal-demo">');
Expand Down
74 changes: 40 additions & 34 deletions compiler/src/test-utils/fixture-test-processor.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
import { spawn } from 'child_process';
import path from 'path';

import { Parent as HastParent } from 'hast';
import { Parent as MDastParent } from 'mdast';
import { VFile } from 'vfile';
import dargs from 'dargs';

import { Options } from '../context';
import { rMarkdown } from '..';

export async function fixtureTestProcessor(
fixture: string,
options: Options = {}
) {
const unit = {
md: '',
files: [] as VFile[],
mdast: {} as MDastParent,
hast: {} as HastParent,
html: '',
};

const [result] = await rMarkdown(path.join('./fixtures', fixture), {
noDoc: true,
noCache: true,
noPdf: true,
noReport: true,
noWrite: true,
noEmbedAssets: true,
...options,
});
const flags = dargs(
{
noDoc: true,
noCache: true,
noPdf: true,
noReport: true,
noWrite: true,
noEmbedAssets: true,
output: 'md',
verbose: true,
...options,
},
{ allowCamelCase: true }
);

return new Promise<string>((resolve, reject) => {
const args = [path.join('./fixtures', fixture), ...flags];
const rmarkdown = spawn('rmarkdown', args);

const result: string[] = [];

rmarkdown.stdout.on('data', (data) => {
const str = data.toString();

unit.md = result.md;
unit.files = result.files;
if (result.html) {
unit.mdast = result.html.mdast;
unit.hast = result.html.hast;
unit.html = result.html.html;
} else {
console.log(
'[test processor]: no html object returned from buildUnit'
);
}

return unit;
if (str.startsWith('✖')) {
return reject(str);
}

result.push(str);
});

rmarkdown.stdout.on('end', () => {
resolve(result.join(''));
});

rmarkdown.stderr.on('data', (data) => {
reject(data.toString());
});
});
}
24 changes: 21 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a10935

Please sign in to comment.