Skip to content

Commit

Permalink
fixes issue with creating main.brs file that would cause the entire p…
Browse files Browse the repository at this point in the history
…roject to go south (#207)
  • Loading branch information
georgejecook authored Dec 22, 2022
1 parent 567940c commit a467185
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
33 changes: 19 additions & 14 deletions bsc-plugin/src/lib/rooibos/RooibosSession.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'path';
import type { BrsFile, ClassStatement, FunctionStatement, NamespaceStatement, Program, ProgramBuilder } from 'brighterscript';
import { util } from 'brighterscript';
import { isBrsFile, ParseMode } from 'brighterscript';
import type { AstEditor } from 'brighterscript/dist/astUtils/AstEditor';
import type { RooibosConfig } from './RooibosConfig';
Expand All @@ -11,6 +12,7 @@ import type { TestSuite } from './TestSuite';
import { diagnosticErrorNoMainFound as diagnosticWarnNoMainFound } from '../utils/Diagnostics';
import undent from 'undent';
import { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';
import * as fsExtra from 'fs-extra';

// eslint-disable-next-line
const pkg = require('../../../package.json');
Expand Down Expand Up @@ -44,9 +46,7 @@ export class RooibosSession {
return testSuites.length > 0;
}

private rooibosMain: BrsFile;

public addLaunchHook(editor: AstEditor) {
public addLaunchHookToExistingMain(editor: AstEditor) {
let mainFunction: FunctionStatement;
const files = this._builder.program.getScopeByName('source').getOwnFiles();
for (let file of files) {
Expand All @@ -60,19 +60,24 @@ export class RooibosSession {
}
if (mainFunction) {
editor.addToArray(mainFunction.func.body.statements, 0, new RawCodeStatement(`Rooibos_init()`));
} else {
diagnosticWarnNoMainFound(files[0]);
this.rooibosMain = this._builder.program.setFile('source/rooibosMain.brs', `function main()\n Rooibos_init()\nend function`);
}
}

/**
* Should only be called in afterProgramTranspile to remove the rooibosMain file IF it exists
*/
public removeRooibosMain() {
if (this.rooibosMain) {
this._builder.program.removeFile('source/rooibosMain.brs');
this.rooibosMain = undefined;
public addLaunchHookFileIfNotPresent() {
let mainFunction: FunctionStatement;
const files = this._builder.program.getScopeByName('source').getOwnFiles();
for (let file of files) {
if (isBrsFile(file)) {
const mainFunc = file.parser.references.functionStatements.find((f) => f.name.text.toLowerCase() === 'main');
if (mainFunc) {
mainFunction = mainFunc;
break;
}
}
}
if (!mainFunction) {
diagnosticWarnNoMainFound(files[0]);
const filePath = path.join(this._builder.options.stagingDir, 'source/rooibosMain.brs');
fsExtra.writeFileSync(filePath, `function main()\n Rooibos_init()\nend function`);
}
}

Expand Down
53 changes: 51 additions & 2 deletions bsc-plugin/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('RooibosPlugin', () => {
plugin = new RooibosPlugin();
options = {
rootDir: _rootDir,
stagingFolderPath: _stagingFolderPath
stagingFolderPath: _stagingFolderPath,
stagingDir: _stagingFolderPath
};
fsExtra.ensureDirSync(_stagingFolderPath);
fsExtra.ensureDirSync(_rootDir);
Expand Down Expand Up @@ -574,9 +575,57 @@ describe('RooibosPlugin', () => {
end class
`);
program.validate();
expect(program.getDiagnostics()).to.be.empty;
await builder.transpile();
expect(program.getDiagnostics().filter((d) => d.code !== 'RBS2213')).to.be.empty;
expect(plugin.session.sessionInfo.testSuitesToRun).to.not.be.empty;
expect(
getTestFunctionContents(true)
).to.eql(undent`
m.currentAssertLineNumber = 6
m._expectCalled(m.thing, "getFunction", m, "m.thing", [])
if m.currentResult.isFail then return invalid
m.currentAssertLineNumber = 7
m._expectCalled(m.thing, "getFunction", m, "m.thing", [], "return")
if m.currentResult.isFail then return invalid
m.currentAssertLineNumber = 8
m._expectCalled(m.thing, "getFunction", m, "m.thing", [
"arg1"
"arg2"
])
if m.currentResult.isFail then return invalid
m.currentAssertLineNumber = 9
m._expectCalled(m.thing, "getFunction", m, "m.thing", [
"arg1"
"arg2"
], "return")
if m.currentResult.isFail then return invalid
`);
});
it('does not break when validating again after a transpile', async () => {
program.setFile('source/test.spec.bs', `
@suite
class ATest
@describe("groupA")
@it("test1")
function _()
m.expectCalled(m.thing.getFunction())
m.expectCalled(m.thing.getFunction(), "return")
m.expectCalled(m.thing.getFunction("arg1", "arg2"))
m.expectCalled(m.thing.getFunction("arg1", "arg2"), "return")
end function
end class
`);
program.validate();
await builder.transpile();
program.validate();
expect(program.getDiagnostics().filter((d) => d.code !== 'RBS2213')).to.be.empty;
expect(plugin.session.sessionInfo.testSuitesToRun).to.not.be.empty;
expect(
getTestFunctionContents(true)
).to.eql(undent`
Expand Down
12 changes: 6 additions & 6 deletions bsc-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class RooibosPlugin implements CompilerPlugin {
return;
}

// console.log('processing ', file.pkgPath);
console.log('processing ', file.pkgPath);
if (isBrsFile(file)) {
if (this.session.processFile(file)) {
//
Expand All @@ -95,7 +95,11 @@ export class RooibosPlugin implements CompilerPlugin {

beforeProgramTranspile(program: Program, entries: TranspileObj[], editor: AstEditor) {
this.session.addTestRunnerMetadata(editor);
this.session.addLaunchHook(editor);
this.session.addLaunchHookToExistingMain(editor);
}

afterProgramTranspile(program: Program, entries: TranspileObj[], editor: AstEditor) {
this.session.addLaunchHookFileIfNotPresent();
}

beforeFileTranspile(event: BeforeFileTranspileEvent) {
Expand All @@ -118,10 +122,6 @@ export class RooibosPlugin implements CompilerPlugin {
}
}

afterProgramTranspile(program: Program, entries: TranspileObj[], editor: AstEditor) {
this.session.removeRooibosMain();
}

afterProgramValidate(program: Program) {
// console.log('bpv');
this.session.updateSessionStats();
Expand Down

0 comments on commit a467185

Please sign in to comment.