From 8226ea784edc7a14b496fafc9bf3cb52ade9d179 Mon Sep 17 00:00:00 2001 From: Aaron Abramov Date: Mon, 14 Aug 2017 10:59:26 -0700 Subject: [PATCH] flowtype integration_tests/utils.js (#4261) --- integration_tests/utils.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/integration_tests/utils.js b/integration_tests/utils.js index 206fd52a780d..5fb658dfb975 100644 --- a/integration_tests/utils.js +++ b/integration_tests/utils.js @@ -4,16 +4,21 @@ * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. + * + * @flow */ + 'use strict'; +import type {Path} from 'types/Config'; + const {spawnSync} = require('child_process'); const fs = require('fs'); const path = require('path'); const mkdirp = require('mkdirp'); const rimraf = require('rimraf'); -const run = (cmd, cwd) => { +const run = (cmd: string, cwd?: Path) => { const args = cmd.split(/\s/).slice(1); const spawnOptions = {cwd}; const result = spawnSync(cmd.split(/\s/)[0], args, spawnOptions); @@ -24,7 +29,7 @@ const run = (cmd, cwd) => { STDOUT: ${result.stdout && result.stdout.toString()} STDERR: ${result.stderr && result.stderr.toString()} STATUS: ${result.status} - ERROR: ${result.error} + ERROR: ${result.error && result.error.toString()} `; throw new Error(message); } @@ -35,7 +40,7 @@ const run = (cmd, cwd) => { return result; }; -const linkJestPackage = (packageName, cwd) => { +const linkJestPackage = (packageName: string, cwd: Path) => { const packagesDir = path.resolve(__dirname, '../packages'); const packagePath = path.resolve(packagesDir, packageName); const destination = path.resolve(cwd, 'node_modules/'); @@ -43,7 +48,7 @@ const linkJestPackage = (packageName, cwd) => { return run(`ln -sf ${packagePath} ${destination}`); }; -const fileExists = filePath => { +const fileExists = (filePath: Path) => { try { fs.accessSync(filePath, fs.F_OK); return true; @@ -52,9 +57,12 @@ const fileExists = filePath => { } }; -const makeTemplate = string => { - return values => { - return string.replace(/\$(\d+)/g, (match, number) => { +const makeTemplate = (str: string): ((values?: Array) => string) => { + return (values: ?Array) => { + return str.replace(/\$(\d+)/g, (match, number) => { + if (!Array.isArray(values)) { + throw new Error('Array of values must be passed to the template.'); + } return values[number - 1]; }); }; @@ -102,7 +110,10 @@ const copyDir = (src: string, dest: string) => { } }; -const createEmptyPackage = (directory, packageJson) => { +const createEmptyPackage = ( + directory: Path, + packageJson?: {[keys: string]: any}, +) => { const DEFAULT_PACKAGE_JSON = { description: 'THIS IS AN AUTOGENERATED FILE AND SHOULD NOT BE ADDED TO GIT', jest: { @@ -118,7 +129,7 @@ const createEmptyPackage = (directory, packageJson) => { ); }; -const extractSummary = stdout => { +const extractSummary = (stdout: string) => { const match = stdout.match( /Test Suites:.*\nTests.*\nSnapshots.*\nTime.*(\nRan all test suites)*.*\n*$/gm, );