-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: set up a bootstrap script for all jasmine tests which ensures …
…that the temp directory is cleared Creating a bootstrap script for jasmine tests allows us to ensure automatically for all jasmine tests that the temporary directory created for the test runs is cleaned between specs.
- Loading branch information
1 parent
405f2b3
commit dbc0cd7
Showing
7 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
load("@npm//@bazel/typescript:index.bzl", "ts_library") | ||
load("//tools:defaults.bzl", "jasmine_node_test") | ||
|
||
ts_library( | ||
name = "bootstrap_ts", | ||
testonly = True, | ||
srcs = [ | ||
"bootstrap.ts", | ||
], | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//ng-dev/utils/testing:bazel-env", | ||
"@npm//@bazel/jasmine", | ||
"@npm//@types/jasmine", | ||
], | ||
) | ||
|
||
alias( | ||
name = "bootstrap_dependencies", | ||
actual = "//ng-dev/utils/testing:bazel-env", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
filegroup( | ||
name = "bootstrap", | ||
testonly = True, | ||
srcs = [":bootstrap_ts"], | ||
output_group = "es5_sources", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
ts_library( | ||
name = "bootstrap_test", | ||
testonly = True, | ||
srcs = [ | ||
"bootstrap.spec.ts", | ||
], | ||
deps = [ | ||
":bootstrap_ts", | ||
"//ng-dev/utils/testing:bazel-env", | ||
"@npm//@types/node", | ||
], | ||
) | ||
|
||
jasmine_node_test( | ||
name = "test", | ||
specs = [ | ||
":bootstrap_test", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {readFileSync, writeFileSync} from 'fs'; | ||
import {join} from 'path'; | ||
import {testTmpDir} from '../../ng-dev/utils/testing/bazel-env'; | ||
|
||
describe('bootstraping script', () => { | ||
// This specific describe block is explicitly testing that | ||
jasmine.getEnv().configure({ | ||
...jasmine.getEnv().configuration(), | ||
random: false, | ||
}); | ||
|
||
/** A randomized string to write into the file to confirm it was removed. */ | ||
const testContent = `This is random: ${Math.random()}`; | ||
/** The absolute path to a file within the temporary test directory. */ | ||
const testFilePath = join(testTmpDir, 'test-file'); | ||
/** The number of tests which have run within the describe block. */ | ||
let testCount = 0; | ||
|
||
it('allows files in the temp directory to be modified', () => { | ||
// The postfix increment operator is used to check the current value, while incrementing by 1. | ||
expect(testCount++).toBe(0); | ||
// Write the test content to the a file in the temporary test directory. | ||
writeFileSync(testFilePath, testContent); | ||
|
||
expect(readFileSync(testFilePath, {encoding: 'utf8'})).toBe(testContent); | ||
}); | ||
|
||
it('ensures the files created in previous tests are not kept between specs', () => { | ||
// The postfix increment operator is used to check the current value, while incrementing by 1. | ||
expect(testCount++).toBe(1); | ||
/** A matcher for a file not existing at the path provided. */ | ||
const errorMatcher = new RegExp(/^ENOENT\: no such file or directory, open/); | ||
|
||
expect(() => readFileSync(testFilePath, {encoding: 'utf8'})).toThrowError(errorMatcher); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
import {cleanTestTmpDir} from '../../ng-dev/utils/testing/bazel-env'; | ||
|
||
/** | ||
* The `boot` utility function from `@bazel/jasmine` allows initialization before the runner, with | ||
* protections to prevent calling `boot` again. | ||
*/ | ||
const {boot} = require('@bazel/jasmine'); | ||
|
||
// Initialize jasmine, because beforeEach and other jasmine "globals" are not defined in the | ||
// environment until jasmine has been initialized. Since our bootstrap explicitly is meant to use | ||
// these globals, we need to ensure that jasmine has already initialized before the major content | ||
// of our script executes. | ||
boot(); | ||
|
||
beforeEach(() => { | ||
// Before each spec runs, clean the temporary test dir to prevent specs from effecting each other. | ||
cleanTestTmpDir(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@npm//@bazel/jasmine:index.bzl", _jasmine_node_test = "jasmine_node_test") | ||
|
||
def jasmine_node_test(name, specs = [], **kwargs): | ||
templated_args = kwargs.pop("templated_args", []) + [ | ||
# Include our jasmime bootstrap file to be run before the jasmine runner. | ||
"--node_options=--require=$$(rlocation $(rootpath //tools/jasmine:bootstrap))", | ||
] | ||
|
||
_jasmine_node_test( | ||
name = name, | ||
srcs = kwargs.pop("srcs", []) + specs, | ||
use_direct_specs = True, | ||
deps = kwargs.pop("deps", []) + [ | ||
"//tools/jasmine:bootstrap", | ||
# Because we don't provided a bundled script, we must ensure that the dependencies for | ||
# bootstrap are also included as dependencies. | ||
"//tools/jasmine:bootstrap_dependencies", | ||
], | ||
templated_args = templated_args, | ||
**kwargs | ||
) |