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

build: dont run karma if compilation failed #3668

Merged
merged 3 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions tools/gulp/tasks/unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ gulp.task('test', [':test:deps'], () => {
});

// Refreshes Karma's file list and schedules a test run.
Copy link
Member

Choose a reason for hiding this comment

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

Expand comment to say something like "Only run tests if TypeScript compilation is successful" ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. Anything else?

let runTests = () => {
server.refreshFiles().then(() => server._injector.get('executor').schedule());
let runTests = (err?: Error) => {
if (!err) {
server.refreshFiles().then(() => server._injector.get('executor').schedule());
}
};

// Boot up the test server and run the tests whenever a new browser connects.
server.start();
server.on('browser_register', runTests);
server.on('browser_register', () => runTests());
Copy link
Member

Choose a reason for hiding this comment

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

Why the extra level of wrapping here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because this event comes from Karma and it calls the callback with the browser_data as first argument.


// Watch for file changes, rebuild and run the tests.
gulp.watch(patternRoot + '.ts', () => runSequence(':build:components:ts:spec', runTests));
Expand Down
4 changes: 1 addition & 3 deletions tools/gulp/util/task_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ function _globify(maybeGlob: string, suffix = '**/*') {

/** Creates a task that runs the TypeScript compiler */
export function tsBuildTask(tsConfigPath: string, extraOptions?: CompilerOptions) {
return () => {
compileProject(tsConfigPath, extraOptions);
};
return () => compileProject(tsConfigPath, extraOptions);
}


Expand Down
23 changes: 12 additions & 11 deletions tools/gulp/util/ts-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import * as chalk from 'chalk';
export function compileProject(project: string, options: ts.CompilerOptions) {
let parsed = parseProjectConfig(project, options);
let program = ts.createProgram(parsed.fileNames, parsed.options);
let baseDir = program.getCurrentDirectory();

// Report any invalid TypeScript options for the project.
checkDiagnostics(program.getOptionsDiagnostics());
reportDiagnostics(program.getOptionsDiagnostics(), baseDir);

let emitResult = program.emit();

checkDiagnostics(emitResult.diagnostics);
reportDiagnostics(emitResult.diagnostics, baseDir);
}

/** Parses a TypeScript project configuration. */
Expand All @@ -31,26 +32,26 @@ function parseProjectConfig(project: string, options: ts.CompilerOptions) {
}

/** Formats the TypeScript diagnostics into a error string. */
export function formatDiagnostics(diagnostics: ts.Diagnostic[]): string {
export function formatDiagnostics(diagnostics: ts.Diagnostic[], baseDir: string): string {
return diagnostics.map(diagnostic => {
let res = ts.DiagnosticCategory[diagnostic.category];
let res = `• ${chalk.red(`TS${diagnostic.code}`)} - `;

if (diagnostic.file) {
let {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
let filePath = path.relative(baseDir, diagnostic.file.fileName);

res += ` at ${diagnostic.file.fileName}(${line + 1},${character + 1}):`;
res += `${filePath}(${line + 1},${character + 1}): `;
}
res += ` ${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`;
res += `${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`;

return res;
}).join('\n');
}

/** Checks diagnostics and throws errors if present. */
export function checkDiagnostics(diagnostics: ts.Diagnostic[]) {
/** Checks and reports diagnostics if present. */
export function reportDiagnostics(diagnostics: ts.Diagnostic[], baseDir?: string) {
if (diagnostics && diagnostics.length && diagnostics[0]) {
console.error(formatDiagnostics(diagnostics));
console.error(chalk.red('TypeScript compilation failed. Exiting process.'));
process.exit(1);
console.error(formatDiagnostics(diagnostics, baseDir));
throw new Error('TypeScript compilation failed.');
}
}