Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Rooibos_init injection causing duplicate calls #247

Merged
Merged
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
10 changes: 7 additions & 3 deletions bsc-plugin/src/lib/rooibos/RooibosSession.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from 'path';
import type { BrsFile, BscFile, ClassStatement, FunctionStatement, NamespaceStatement, Program, ProgramBuilder, Scope, Statement } from 'brighterscript';
import { isBrsFile, ParseMode, util } from 'brighterscript';
import { isBrsFile, isCallExpression, isVariableExpression, ParseMode, WalkMode } from 'brighterscript';
import type { AstEditor } from 'brighterscript/dist/astUtils/AstEditor';
import type { RooibosConfig } from './RooibosConfig';
import { SessionInfo } from './RooibosSessionInfo';
Expand All @@ -10,7 +10,6 @@ import type { FileFactory } from './FileFactory';
import type { TestSuite } from './TestSuite';
import { diagnosticErrorNoMainFound as diagnosticWarnNoMainFound, diagnosticNoStagingDir } from '../utils/Diagnostics';
import undent from 'undent';
import { BrsTranspileState } from 'brighterscript/dist/parser/BrsTranspileState';
import * as fsExtra from 'fs-extra';
import type { MockUtil } from './MockUtil';

Expand Down Expand Up @@ -89,7 +88,12 @@ export class RooibosSession {
}
}
if (mainFunction) {
editor.addToArray(mainFunction.func.body.statements, 0, new RawCodeStatement(`Rooibos_init("${this.config?.testSceneName ?? 'RooibosScene'}")`));
const initCall = mainFunction.func.body.findChild(f => isCallExpression(f) && isVariableExpression(f.callee) && f.callee.name.text.toLowerCase() === 'rooibos_init', {
walkMode: WalkMode.visitAllRecursive
});
if (!initCall) {
editor.addToArray(mainFunction.func.body.statements, 0, new RawCodeStatement(`Rooibos_init("${this.config?.testSceneName ?? 'RooibosScene'}")`));
Copy link
Contributor Author

@luis-soares-sky luis-soares-sky Jan 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've considered changing this line to use codegen methods instead of RawCodeStatement, but decided not to in order to keep the PR small and concise. It's something that could be considered in a future refactor if we want more safety.

Suggested change
editor.addToArray(mainFunction.func.body.statements, 0, new RawCodeStatement(`Rooibos_init("${this.config?.testSceneName ?? 'RooibosScene'}")`));
editor.addToArray(mainFunction.func.body.statements, 0, new ExpressionStatement(createCall(
createVariableExpression('rooibos_init'),
[
createVariableExpression(`"${this.config?.testSceneName ?? 'RooibosScene'}"`)
]
)));

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not a bad idea. @TwitchBronBron and I where looking today to move the modifyAssertions to proper ast transformations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree; good idea, we can revisit in another pr.

}
}
}
addLaunchHookFileIfNotPresent() {
Expand Down