Skip to content

Commit

Permalink
fix(core): uses lifecycle hooks correctly, so as to not transpile fil…
Browse files Browse the repository at this point in the history
…es before enums and other bsc plugin tasks have occurred (#175)
  • Loading branch information
georgejecook authored May 25, 2022
1 parent 1d3b0a8 commit 991ebd3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
39 changes: 22 additions & 17 deletions bsc-plugin/src/lib/rooibos/RooibosSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,26 +147,31 @@ export class RooibosSession {
}

public createNodeFiles(program: Program) {
let p = path.join('components', 'rooibos', 'generated');

for (let suite of this.sessionInfo.testSuitesToRun.filter((s) => s.isNodeTest)) {
let xmlText = this.getNodeTestXmlText(suite);
let bsPath = path.join(p, `${suite.generatedNodeName}.bs`);
this.fileFactory.addFile(program, path.join(p, `${suite.generatedNodeName}.xml`), xmlText);
let bsFile = program.getFile(bsPath);
if (bsFile) {
(bsFile as BrsFile).parser.statements.push();
bsFile.needsTranspiled = true;
}
let brsFile = this.fileFactory.addFile(program, bsPath, undent`
import "pkg:/${suite.file.pkgPath}"
function init()
nodeRunner = Rooibos_TestRunner(m.top.getScene(), m)
m.top.rooibosTestResult = nodeRunner.runInNodeMode("${suite.name}")
end function
`);
brsFile.parser.invalidateReferences();
this.createNodeFile(program, suite);
}
}

createNodeFile(program: Program, suite: TestSuite) {
let p = path.join('components', 'rooibos', 'generated');

let xmlText = this.getNodeTestXmlText(suite);
let bsPath = path.join(p, `${suite.generatedNodeName}.bs`);
this.fileFactory.addFile(program, path.join(p, `${suite.generatedNodeName}.xml`), xmlText);
let bsFile = program.getFile(bsPath);
if (bsFile) {
(bsFile as BrsFile).parser.statements.push();
bsFile.needsTranspiled = true;
}
let brsFile = this.fileFactory.addFile(program, bsPath, undent`
import "pkg:/${suite.file.pkgPath}"
function init()
nodeRunner = Rooibos_TestRunner(m.top.getScene(), m)
m.top.rooibosTestResult = nodeRunner.runInNodeMode("${suite.name}")
end function
`);
brsFile.parser.invalidateReferences();
}

private getNodeTestXmlText(suite: TestSuite) {
Expand Down
43 changes: 39 additions & 4 deletions bsc-plugin/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ describe('RooibosPlugin', () => {
builder.options = util.normalizeAndResolveConfig(options);
builder.program = new Program(builder.options);
program = builder.program;
builder.plugins = new PluginInterface([plugin], builder.logger);
program.plugins = new PluginInterface([plugin], builder.logger);
program.plugins.add(plugin);
program.createSourceScope(); //ensure source scope is created
plugin.beforeProgramCreate(builder);
plugin.fileFactory['options'].frameworkSourcePath = path.resolve(path.join('../framework/src/source'));
Expand Down Expand Up @@ -609,6 +608,41 @@ describe('RooibosPlugin', () => {
`);
});

it('correctly transpiles enums in assertions', async () => {
program.setFile('source/test.spec.bs', `
namespace lib
enum myEnum
value = "value"
end enum
end namespace
@suite
class ATest
@describe("groupA")
@it("test1")
function _()
b = { someValue: lib.myEnum.value}
m.assertEqual(a, { someValue: lib.myEnum.value})
end function
end class
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
// expect(plugin.session.sessionInfo.testSuitesToRun).to.not.be.empty;
await builder.transpile();
expect(
getTestFunctionContents(true)
).to.eql(undent`
b = {
someValue: "value"
}
m.currentAssertLineNumber = 12
m.assertEqual(a, {
someValue: "value"
})
if m.currentResult.isFail then return invalid`);
});

it('correctly transpiles function invocations - simple object', async () => {
program.setFile('source/test.spec.bs', `
@suite
Expand Down Expand Up @@ -1034,8 +1068,9 @@ describe('RooibosPlugin', () => {
builder.options = util.normalizeAndResolveConfig(options);
builder.program = new Program(builder.options);
program = builder.program;
builder.plugins = new PluginInterface([plugin], builder.logger);
program.plugins = new PluginInterface([plugin], builder.logger);
builder.program = new Program(builder.options);
program = builder.program;
program.plugins.add(plugin);
program.createSourceScope(); //ensure source scope is created
plugin.beforeProgramCreate(builder);
plugin.fileFactory['options'].frameworkSourcePath = path.resolve(path.join('../framework/src/source'));
Expand Down
17 changes: 13 additions & 4 deletions bsc-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import type {
Program,
ProgramBuilder,
TranspileObj,
AstEditor
AstEditor,
BeforeFileTranspileEvent,
PluginHandler
} from 'brighterscript';
import {
isBrsFile
Expand Down Expand Up @@ -96,18 +98,25 @@ export class RooibosPlugin implements CompilerPlugin {
beforeProgramTranspile(program: Program, entries: TranspileObj[], editor: AstEditor) {
this.session.addTestRunnerMetadata(editor);
this.session.addLaunchHook(editor);
for (let testSuite of [...this.session.sessionInfo.testSuitesToRun.values()]) {
}

beforeFileTranspile(event: BeforeFileTranspileEvent) {
let testSuite = this.session.sessionInfo.testSuitesToRun.find((ts) => ts.file.pkgPath === event.file.pkgPath);
if (testSuite) {
let noEarlyExit = testSuite.annotation.noEarlyExit;
if (noEarlyExit) {
console.warn(`WARNING: testSuite "${testSuite.name}" is marked as noEarlyExit`);
}

testSuite.addDataFunctions(editor);
testSuite.addDataFunctions(event.editor as any);
for (let group of [...testSuite.testGroups.values()].filter((tg) => tg.isIncluded)) {
for (let testCase of [...group.testCases.values()].filter((tc) => tc.isIncluded)) {
group.modifyAssertions(testCase, noEarlyExit, editor);
group.modifyAssertions(testCase, noEarlyExit, event.editor as any);
}
}
if (testSuite.isNodeTest) {
this.session.createNodeFile(event.program, testSuite);
}
}

this.session.createNodeFiles(this._builder.program);
Expand Down

0 comments on commit 991ebd3

Please sign in to comment.