From aaac83d126886ea52cc49d321e3fa80822ea7972 Mon Sep 17 00:00:00 2001 From: Ika Date: Thu, 7 Sep 2017 13:16:45 +0800 Subject: [PATCH] refactor(runPrettier): reduce duplicate code (#2764) --- tests_integration/runPrettier.js | 54 ++++++++++++++------------------ 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/tests_integration/runPrettier.js b/tests_integration/runPrettier.js index ce91e596..2782dfc2 100644 --- a/tests_integration/runPrettier.js +++ b/tests_integration/runPrettier.js @@ -1,7 +1,7 @@ "use strict"; -const path = require("path"); const fs = require("fs"); +const path = require("path"); function runPrettier(dir, args, options) { let status; @@ -16,39 +16,19 @@ function runPrettier(dir, args, options) { }); const spiedStdoutWrite = jest.spyOn(process.stdout, "write"); - spiedStdoutWrite.mockImplementation(text => { - if (status === undefined) { - stdout += text; - } - }); + spiedStdoutWrite.mockImplementation(text => appendStdout(text)); const spiedStderrWrite = jest.spyOn(process.stderr, "write"); - spiedStderrWrite.mockImplementation(text => { - if (status === undefined) { - stderr += text; - } - }); + spiedStderrWrite.mockImplementation(text => appendStderr(text)); const spiedConsoleLog = jest.spyOn(console, "log"); - spiedConsoleLog.mockImplementation(text => { - if (status === undefined) { - stdout += text + "\n"; - } - }); + spiedConsoleLog.mockImplementation(text => appendStdout(text + "\n")); const spiedConsoleWarn = jest.spyOn(console, "warn"); - spiedConsoleWarn.mockImplementation(text => { - if (status === undefined) { - stderr += text + "\n"; - } - }); + spiedConsoleWarn.mockImplementation(text => appendStderr(text + "\n")); const spiedConsoleError = jest.spyOn(console, "error"); - spiedConsoleError.mockImplementation(text => { - if (status === undefined) { - stderr += text + "\n"; - } - }); + spiedConsoleError.mockImplementation(text => appendStderr(text + "\n")); const write = []; @@ -58,9 +38,9 @@ function runPrettier(dir, args, options) { }); const originalCwd = process.cwd(); - const originalIsTTY = process.stdin.isTTY; const originalArgv = process.argv; const originalExitCode = process.exitCode; + const originalStdinIsTTY = process.stdin.isTTY; process.chdir(normalizeDir(dir)); process.stdin.isTTY = false; @@ -75,14 +55,15 @@ function runPrettier(dir, args, options) { require("../bin/prettier"); status = status || process.exitCode || 0; } catch (error) { - stderr += error.message; status = 1; + stderr += error.message; } finally { - process.exitCode = originalExitCode; - process.stdin.isTTY = originalIsTTY; - process.argv = originalArgv; process.chdir(originalCwd); + process.argv = originalArgv; + process.exitCode = originalExitCode; + process.stdin.isTTY = originalStdinIsTTY; + // TODO: use `jest.restoreAllMocks()` once facebook/jest#4436 fixed spiedProcessExit.mockRestore(); spiedStdoutWrite.mockRestore(); spiedStderrWrite.mockRestore(); @@ -93,6 +74,17 @@ function runPrettier(dir, args, options) { } return { status, stdout, stderr, write }; + + function appendStdout(text) { + if (status === undefined) { + stdout += text; + } + } + function appendStderr(text) { + if (status === undefined) { + stderr += text; + } + } } function normalizeDir(dir) {