-
-
Notifications
You must be signed in to change notification settings - Fork 197
fix: validate test init before executing the test command #4381
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,75 @@ | ||
import * as helpers from "../common/helpers"; | ||
|
||
function RunKarmaTestCommandFactory(platform: string) { | ||
return function RunKarmaTestCommand($options: IOptions, $testExecutionService: ITestExecutionService, $projectData: IProjectData, $analyticsService: IAnalyticsService, $platformEnvironmentRequirements: IPlatformEnvironmentRequirements) { | ||
$projectData.initializeProjectData(); | ||
$analyticsService.setShouldDispose($options.justlaunch || !$options.watch); | ||
const projectFilesConfig = helpers.getProjectFilesConfig({ isReleaseBuild: $options.release }); | ||
this.execute = (args: string[]): Promise<void> => $testExecutionService.startKarmaServer(platform, $projectData, projectFilesConfig); | ||
this.canExecute = (args: string[]): Promise<boolean> => canExecute({ $platformEnvironmentRequirements, $projectData, $options, platform }); | ||
this.allowedParameters = []; | ||
}; | ||
} | ||
abstract class TestCommandBase { | ||
public allowedParameters: ICommandParameter[] = []; | ||
private projectFilesConfig: IProjectFilesConfig; | ||
protected abstract platform: string; | ||
protected abstract $projectData: IProjectData; | ||
protected abstract $testExecutionService: ITestExecutionService; | ||
protected abstract $analyticsService: IAnalyticsService; | ||
protected abstract $options: IOptions; | ||
protected abstract $platformEnvironmentRequirements: IPlatformEnvironmentRequirements; | ||
protected abstract $errors: IErrors; | ||
|
||
async execute(args: string[]): Promise<void> { | ||
await this.$testExecutionService.startKarmaServer(this.platform, this.$projectData, this.projectFilesConfig); | ||
} | ||
|
||
async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> { | ||
this.$projectData.initializeProjectData(); | ||
this.$analyticsService.setShouldDispose(this.$options.justlaunch || !this.$options.watch); | ||
this.projectFilesConfig = helpers.getProjectFilesConfig({ isReleaseBuild: this.$options.release }); | ||
|
||
async function canExecute(input: { $platformEnvironmentRequirements: IPlatformEnvironmentRequirements, $projectData: IProjectData, $options: IOptions, platform: string }): Promise<boolean> { | ||
const { $platformEnvironmentRequirements, $projectData, $options, platform } = input; | ||
const output = await $platformEnvironmentRequirements.checkEnvironmentRequirements({ | ||
platform, | ||
projectDir: $projectData.projectDir, | ||
options: $options, | ||
notConfiguredEnvOptions: { | ||
hideSyncToPreviewAppOption: true, | ||
hideCloudBuildOption: true | ||
const output = await this.$platformEnvironmentRequirements.checkEnvironmentRequirements({ | ||
platform: this.platform, | ||
projectDir: this.$projectData.projectDir, | ||
options: this.$options, | ||
notConfiguredEnvOptions: { | ||
hideSyncToPreviewAppOption: true, | ||
hideCloudBuildOption: true | ||
} | ||
}); | ||
|
||
const canStartKarmaServer = await this.$testExecutionService.canStartKarmaServer(this.$projectData); | ||
if (!canStartKarmaServer) { | ||
this.$errors.fail({ | ||
formatStr: "Error: In order to run unit tests, your project must already be configured by running $ tns test init.", | ||
suppressCommandHelp: true, | ||
errorCode: ErrorCodes.TESTS_INIT_REQUIRED | ||
}); | ||
} | ||
}); | ||
|
||
return output.canExecute; | ||
return output.canExecute && canStartKarmaServer; | ||
} | ||
} | ||
|
||
class TestAndroidCommand extends TestCommandBase implements ICommand { | ||
protected platform = "android"; | ||
|
||
constructor(protected $projectData: IProjectData, | ||
protected $testExecutionService: ITestExecutionService, | ||
protected $analyticsService: IAnalyticsService, | ||
protected $options: IOptions, | ||
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements, | ||
protected $errors: IErrors) { | ||
super(); | ||
} | ||
|
||
} | ||
|
||
class TestIosCommand extends TestCommandBase implements ICommand { | ||
protected platform = "iOS"; | ||
|
||
constructor(protected $projectData: IProjectData, | ||
protected $testExecutionService: ITestExecutionService, | ||
protected $analyticsService: IAnalyticsService, | ||
protected $options: IOptions, | ||
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements, | ||
protected $errors: IErrors) { | ||
super(); | ||
} | ||
|
||
} | ||
|
||
$injector.registerCommand("test|android", RunKarmaTestCommandFactory('android')); | ||
$injector.registerCommand("test|ios", RunKarmaTestCommandFactory('iOS')); | ||
$injector.registerCommand("test|android", TestAndroidCommand); | ||
$injector.registerCommand("test|ios", TestIosCommand); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import * as util from "util"; | ||
import * as path from "path"; | ||
import { SourceMapConsumer } from "source-map"; | ||
import { isInteractive } from "./helpers"; | ||
|
||
// we need this to overwrite .stack property (read-only in Error) | ||
function Exception() { | ||
|
@@ -159,7 +160,7 @@ export class Errors implements IErrors { | |
} catch (ex) { | ||
const loggerLevel: string = $injector.resolve("logger").getLevel().toUpperCase(); | ||
const printCallStack = this.printCallStack || loggerLevel === "TRACE" || loggerLevel === "DEBUG"; | ||
const message = printCallStack ? resolveCallStack(ex) : `\x1B[31;1m${ex.message}\x1B[0m`; | ||
const message = printCallStack ? resolveCallStack(ex) : isInteractive() ? `\x1B[31;1m${ex.message}\x1B[0m` : ex.message; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this probably will affect Sidekick/KinveyStudio but I'm not sure what exactly will be the impact. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to split the conditions in two separate lines for readability of the code. |
||
|
||
if (ex.printOnStdout) { | ||
this.$injector.resolve("logger").out(message); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { InjectorStub } from "../stubs"; | ||
import { TestExecutionService } from "../../lib/services/test-execution-service"; | ||
import { assert } from "chai"; | ||
|
||
const karmaPluginName = "karma"; | ||
const unitTestsPluginName = "nativescript-unit-test-runner"; | ||
|
||
function getTestExecutionService(): ITestExecutionService { | ||
const injector = new InjectorStub(); | ||
injector.register("testExecutionService", TestExecutionService); | ||
|
||
return injector.resolve("testExecutionService"); | ||
} | ||
|
||
function getDependenciesObj(deps: string[]): IDictionary<string> { | ||
const depsObj: IDictionary<string> = {}; | ||
deps.forEach(dep => { | ||
depsObj[dep] = "1.0.0"; | ||
}); | ||
|
||
return depsObj; | ||
} | ||
|
||
describe("testExecutionService", () => { | ||
const testCases = [ | ||
{ | ||
name: "should return false when the project has no dependencies and dev dependencies", | ||
expectedCanStartKarmaServer: false, | ||
projectData: { dependencies: {}, devDependencies: {} } | ||
}, | ||
{ | ||
name: "should return false when the project has no karma", | ||
expectedCanStartKarmaServer: false, | ||
projectData: { dependencies: getDependenciesObj([unitTestsPluginName]), devDependencies: {} } | ||
}, | ||
{ | ||
name: "should return false when the project has no unit test runner", | ||
expectedCanStartKarmaServer: false, | ||
projectData: { dependencies: getDependenciesObj([karmaPluginName]), devDependencies: {} } | ||
}, | ||
{ | ||
name: "should return true when the project has the required plugins as dependencies", | ||
expectedCanStartKarmaServer: true, | ||
projectData: { dependencies: getDependenciesObj([karmaPluginName, unitTestsPluginName]), devDependencies: {} } | ||
}, | ||
{ | ||
name: "should return true when the project has the required plugins as dev dependencies", | ||
expectedCanStartKarmaServer: true, | ||
projectData: { dependencies: {}, devDependencies: getDependenciesObj([karmaPluginName, unitTestsPluginName]) } | ||
}, | ||
{ | ||
name: "should return true when the project has the required plugins as dev and normal dependencies", | ||
expectedCanStartKarmaServer: true, | ||
projectData: { dependencies: getDependenciesObj([karmaPluginName]), devDependencies: getDependenciesObj([unitTestsPluginName]) } | ||
} | ||
]; | ||
|
||
describe("canStartKarmaServer", () => { | ||
_.each(testCases, (testCase: any) => { | ||
it(`${testCase.name}`, async () => { | ||
const testExecutionService = getTestExecutionService(); | ||
const canStartKarmaServer = await testExecutionService.canStartKarmaServer(testCase.projectData); | ||
assert.equal(canStartKarmaServer, testCase.expectedCanStartKarmaServer); | ||
}); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can create better user experience by prompt the user to automatically execute
tns test init
command.This is not a merge stopper and can be done in another PR if we decide it is appropriate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#4387