-
Notifications
You must be signed in to change notification settings - Fork 5k
testrunner: make environment a simple class #2769
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
Merged
aslushnikov
merged 10 commits into
microsoft:master
from
aslushnikov:attempt-to-extract-all-environments-into-separate-class
Jun 30, 2020
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5303c8c
testrunner: drop multiple hooks
aslushnikov 29edd6f
make environments simple objects
aslushnikov f8acd83
a bit of renames
aslushnikov 82e2a8b
migrate tests to env objects
aslushnikov 2de949d
drop hook location
aslushnikov b560f11
fix one more thing
aslushnikov 2057e9b
wip
aslushnikov 9a134f2
move to envs
aslushnikov 4cb48da
cut on resources
aslushnikov 33a515c
address comments
aslushnikov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,247 @@ | ||
| /** | ||
| * Copyright 2019 Google Inc. All rights reserved. | ||
| * Modifications copyright (c) Microsoft Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| const utils = require('./utils'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const rm = require('rimraf').sync; | ||
| const {TestServer} = require('../utils/testserver/'); | ||
|
|
||
| class ServerEnvironment { | ||
| async beforeAll(state) { | ||
| const assetsPath = path.join(__dirname, 'assets'); | ||
| const cachedPath = path.join(__dirname, 'assets', 'cached'); | ||
|
|
||
| const port = 8907 + state.parallelIndex * 2; | ||
| state.server = await TestServer.create(assetsPath, port); | ||
| state.server.enableHTTPCache(cachedPath); | ||
| state.server.PORT = port; | ||
| state.server.PREFIX = `http://localhost:${port}`; | ||
| state.server.CROSS_PROCESS_PREFIX = `http://127.0.0.1:${port}`; | ||
| state.server.EMPTY_PAGE = `http://localhost:${port}/empty.html`; | ||
|
|
||
| const httpsPort = port + 1; | ||
| state.httpsServer = await TestServer.createHTTPS(assetsPath, httpsPort); | ||
| state.httpsServer.enableHTTPCache(cachedPath); | ||
| state.httpsServer.PORT = httpsPort; | ||
| state.httpsServer.PREFIX = `https://localhost:${httpsPort}`; | ||
| state.httpsServer.CROSS_PROCESS_PREFIX = `https://127.0.0.1:${httpsPort}`; | ||
| state.httpsServer.EMPTY_PAGE = `https://localhost:${httpsPort}/empty.html`; | ||
| } | ||
|
|
||
| async afterAll({server, httpsServer}) { | ||
| await Promise.all([ | ||
| server.stop(), | ||
| httpsServer.stop(), | ||
| ]); | ||
| } | ||
|
|
||
| async beforeEach(state) { | ||
| state.server.reset(); | ||
| state.httpsServer.reset(); | ||
| } | ||
| } | ||
|
|
||
| class DefaultBrowserOptionsEnvironment { | ||
| constructor(defaultBrowserOptions, dumpLogOnFailure, playwrightPath) { | ||
| this._defaultBrowserOptions = defaultBrowserOptions; | ||
| this._dumpLogOnFailure = dumpLogOnFailure; | ||
| this._playwrightPath = playwrightPath; | ||
| this._loggerSymbol = Symbol('DefaultBrowserOptionsEnvironment.logger'); | ||
| } | ||
|
|
||
| async beforeAll(state) { | ||
| state[this._loggerSymbol] = utils.createTestLogger(this._dumpLogOnFailure, null, 'extra'); | ||
| state.defaultBrowserOptions = { | ||
| ...this._defaultBrowserOptions, | ||
| logger: state[this._loggerSymbol], | ||
| }; | ||
| state.playwrightPath = this._playwrightPath; | ||
| } | ||
|
|
||
| async beforeEach(state, testRun) { | ||
| state[this._loggerSymbol].setTestRun(testRun); | ||
| } | ||
|
|
||
| async afterEach(state) { | ||
| state[this._loggerSymbol].setTestRun(null); | ||
| } | ||
| } | ||
|
|
||
| // simulate globalSetup per browserType that happens only once regardless of TestWorker. | ||
|
Contributor
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. Let's use globalSetup!
Contributor
Author
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. This will go away in the next patch - with first-class artifacts support in test runner. |
||
| const hasBeenCleaned = new Set(); | ||
|
|
||
| class GoldenEnvironment { | ||
| async beforeAll(state) { | ||
| const { OUTPUT_DIR, GOLDEN_DIR } = utils.testOptions(state.browserType); | ||
| if (!hasBeenCleaned.has(state.browserType)) { | ||
| hasBeenCleaned.add(state.browserType); | ||
| if (fs.existsSync(OUTPUT_DIR)) | ||
| rm(OUTPUT_DIR); | ||
| fs.mkdirSync(OUTPUT_DIR, { recursive: true }); | ||
| } | ||
| state.golden = goldenName => ({ goldenPath: GOLDEN_DIR, outputPath: OUTPUT_DIR, goldenName }); | ||
| } | ||
|
|
||
| async afterAll(state) { | ||
| delete state.golden; | ||
| } | ||
|
|
||
| async afterEach(state, testRun) { | ||
| if (state.browser && state.browser.contexts().length !== 0) { | ||
| if (testRun.ok()) | ||
| console.warn(`\nWARNING: test "${testRun.test().fullName()}" (${testRun.test().location()}) did not close all created contexts!\n`); | ||
| await Promise.all(state.browser.contexts().map(context => context.close())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class TraceTestEnvironment { | ||
| static enableForTest(test) { | ||
| test.setTimeout(100000000); | ||
| test.addEnvironment(new TraceTestEnvironment()); | ||
| } | ||
|
|
||
| constructor() { | ||
| this._session = null; | ||
| } | ||
|
|
||
| async beforeEach() { | ||
| const inspector = require('inspector'); | ||
| const fs = require('fs'); | ||
| const util = require('util'); | ||
| const url = require('url'); | ||
| const readFileAsync = util.promisify(fs.readFile.bind(fs)); | ||
| this._session = new inspector.Session(); | ||
| this._session.connect(); | ||
| const postAsync = util.promisify(this._session.post.bind(this._session)); | ||
| await postAsync('Debugger.enable'); | ||
| const setBreakpointCommands = []; | ||
| const N = t.body().toString().split('\n').length; | ||
| const location = t.location(); | ||
| const lines = (await readFileAsync(location.filePath(), 'utf8')).split('\n'); | ||
| for (let line = 0; line < N; ++line) { | ||
| const lineNumber = line + location.lineNumber(); | ||
| setBreakpointCommands.push(postAsync('Debugger.setBreakpointByUrl', { | ||
| url: url.pathToFileURL(location.filePath()), | ||
| lineNumber, | ||
| condition: `console.log('${String(lineNumber + 1).padStart(6, ' ')} | ' + ${JSON.stringify(lines[lineNumber])})`, | ||
| }).catch(e => {})); | ||
| } | ||
| await Promise.all(setBreakpointCommands); | ||
| } | ||
|
|
||
| async afterEach() { | ||
| this._session.disconnect(); | ||
| } | ||
| } | ||
|
|
||
| class PlaywrightEnvironment { | ||
| constructor(playwright) { | ||
| this._playwright = playwright; | ||
| } | ||
|
|
||
| name() { return 'Playwright'; }; | ||
| beforeAll(state) { state.playwright = this._playwright; } | ||
| afterAll(state) { delete state.playwright; } | ||
| } | ||
|
|
||
| class BrowserTypeEnvironment { | ||
| constructor(browserType) { | ||
| this._browserType = browserType; | ||
| } | ||
|
|
||
| async beforeAll(state) { | ||
| // Channel substitute | ||
| let overridenBrowserType = this._browserType; | ||
| if (process.env.PWCHANNEL) { | ||
| const dispatcherScope = new DispatcherScope(); | ||
| const connection = new Connection(); | ||
| dispatcherScope.onmessage = async message => { | ||
| setImmediate(() => connection.send(message)); | ||
| }; | ||
| connection.onmessage = async message => { | ||
| const result = await dispatcherScope.send(message); | ||
| await new Promise(f => setImmediate(f)); | ||
| return result; | ||
| }; | ||
| BrowserTypeDispatcher.from(dispatcherScope, this._browserType); | ||
| overridenBrowserType = await connection.waitForObjectWithKnownName(this._browserType.name()); | ||
| } | ||
| state.browserType = overridenBrowserType; | ||
| } | ||
|
|
||
| async afterAll(state) { | ||
| delete state.browserType; | ||
| } | ||
| } | ||
|
|
||
| class BrowserEnvironment { | ||
| constructor(browserType, launchOptions, dumpLogOnFailure) { | ||
| this._browserType = browserType; | ||
| this._launchOptions = launchOptions; | ||
| this._dumpLogOnFailure = dumpLogOnFailure; | ||
| this._loggerSymbol = Symbol('BrowserEnvironment.logger'); | ||
| } | ||
|
|
||
| name() { return this._browserType.name(); } | ||
|
|
||
| async beforeAll(state) { | ||
| state[this._loggerSymbol] = utils.createTestLogger(this._dumpLogOnFailure); | ||
| state.browser = await this._browserType.launch({ | ||
| ...this._launchOptions, | ||
| logger: state[this._loggerSymbol], | ||
| }); | ||
| } | ||
|
|
||
| async afterAll(state) { | ||
| await state.browser.close(); | ||
| delete state.browser; | ||
| } | ||
|
|
||
| async beforeEach(state, testRun) { | ||
| state[this._loggerSymbol].setTestRun(testRun); | ||
| } | ||
|
|
||
| async afterEach(state, testRun) { | ||
| state[this._loggerSymbol].setTestRun(null); | ||
| } | ||
| } | ||
|
|
||
| class PageEnvironment { | ||
| async beforeEach(state) { | ||
| state.context = await state.browser.newContext(); | ||
| state.page = await state.context.newPage(); | ||
| } | ||
|
|
||
| async afterEach(state) { | ||
| await state.context.close(); | ||
| state.context = null; | ||
| state.page = null; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| ServerEnvironment, | ||
| GoldenEnvironment, | ||
| TraceTestEnvironment, | ||
| DefaultBrowserOptionsEnvironment, | ||
| PlaywrightEnvironment, | ||
| BrowserTypeEnvironment, | ||
| BrowserEnvironment, | ||
| PageEnvironment, | ||
| }; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Missing copyright.
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.
Done