1import * as ts from "./_namespaces/ts"; 2import { FileBasedTest, IO, userSpecifiedRoot } from "./_namespaces/Harness"; 3 4export type TestRunnerKind = CompilerTestKind | FourslashTestKind | "project" | "rwc" | "test262" | "user" | "dt" | "docker"; 5export type CompilerTestKind = "conformance" | "compiler" | "compiler-oh"; 6export type FourslashTestKind = "fourslash" | "fourslash-shims" | "fourslash-shims-pp" | "fourslash-server" | "fourslash-oh"; 7 8/* eslint-disable prefer-const */ 9export let shards = 1; 10export let shardId = 1; 11/* eslint-enable prefer-const */ 12 13// The following have setters as while they're read here in the harness, they're only set in the runner 14export function setShards(count: number) { 15 shards = count; 16} 17export function setShardId(id: number) { 18 shardId = id; 19} 20 21export abstract class RunnerBase { 22 // contains the tests to run 23 public tests: (string | FileBasedTest)[] = []; 24 25 /** Add a source file to the runner's list of tests that need to be initialized with initializeTests */ 26 public addTest(fileName: string) { 27 this.tests.push(fileName); 28 } 29 30 public enumerateFiles(folder: string, regex?: RegExp, options?: { recursive: boolean }): string[] { 31 return ts.map(IO.listFiles(userSpecifiedRoot + folder, regex, { recursive: (options ? options.recursive : false) }), ts.normalizeSlashes); 32 } 33 34 abstract kind(): TestRunnerKind; 35 36 abstract enumerateTestFiles(): (string | FileBasedTest)[]; 37 38 getTestFiles(): ReturnType<this["enumerateTestFiles"]> { 39 const all = this.enumerateTestFiles(); 40 if (shards === 1) { 41 return all as ReturnType<this["enumerateTestFiles"]>; 42 } 43 return all.filter((_val, idx) => idx % shards === (shardId - 1)) as ReturnType<this["enumerateTestFiles"]>; 44 } 45 46 /** The working directory where tests are found. Needed for batch testing where the input path will differ from the output path inside baselines */ 47 public workingDirectory = ""; 48 49 /** Setup the runner's tests so that they are ready to be executed by the harness 50 * The first test should be a describe/it block that sets up the harness's compiler instance appropriately 51 */ 52 public abstract initializeTests(): void; 53 54 /** Replaces instances of full paths with fileNames only */ 55 static removeFullPaths(path: string) { 56 // If its a full path (starts with "C:" or "/") replace with just the filename 57 let fixedPath = /^(\w:|\/)/.test(path) ? ts.getBaseFileName(path) : path; 58 59 // when running in the browser the 'full path' is the host name, shows up in error baselines 60 const localHost = /http:\/localhost:\d+/g; 61 fixedPath = fixedPath.replace(localHost, ""); 62 return fixedPath; 63 } 64}