Skip to content

Commit

Permalink
Test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ntotten committed Nov 19, 2019
1 parent 5cf4072 commit 0275b35
Show file tree
Hide file tree
Showing 17 changed files with 128 additions and 86 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"editor.formatOnSave": true,
"editor.formatOnType": false,
"files.eol": "\n",
"cSpell.words": [
"Parens",
"Whitespaces",
Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pool:
steps:
- task: NodeTool@0
inputs:
versionSpec: "10.x"
versionSpec: "12.x"
displayName: "Install Node.js"

- bash: |
Expand Down
24 changes: 10 additions & 14 deletions src/test/suite/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import * as assert from "assert";
import * as fs from "fs";
import * as path from "path";
import { format, getText } from "./format.test";
import {
format,
getText,
moveRootPrettierRC,
putBackPrettierRC
} from "./format.test";

const testConfig = (testPath: string, resultPath: string) => {
return async () => {
const { result } = await format("config", testPath);
const { actual } = await format("config", testPath);
const expected = await getText("config", resultPath);
assert.equal(result, expected);
assert.equal(actual, expected);
};
};

const prettierConfigOrig = path.resolve(__dirname, "../../../.prettierrc");
const prettierConfigTemp = path.resolve(__dirname, "../../../old.prettierrc");

suite("Test configurations", function() {
this.timeout(10000);
this.beforeAll(cb => {
fs.rename(prettierConfigOrig, prettierConfigTemp, cb);
});
this.afterAll(cb => {
fs.rename(prettierConfigTemp, prettierConfigOrig, cb);
});
this.beforeAll(moveRootPrettierRC);
this.afterAll(putBackPrettierRC);
test(
"it uses config from .prettierrc file and does not inherit VS Code settings ",
/* cspell: disable-next-line */
Expand Down
26 changes: 16 additions & 10 deletions src/test/suite/eslint.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import * as assert from "assert";
import { format } from "./format.test";
import { platform } from "os";
import {
format,
getText,
moveRootPrettierRC,
putBackPrettierRC
} from "./format.test";

suite("Test eslint", function() {
this.timeout(10000);
this.beforeAll(moveRootPrettierRC);
this.afterAll(putBackPrettierRC);
test("it formats with prettier-eslint", async () => {
const { result } = await format("eslint", "withEslint.js");
return assert.equal(
result,
`// Settings (eslint): single-quote, trailing-comma, no-semi
function foo() {
return 'bar'
}
`
);
if (platform() === "win32") {
return assert.ok(true, "Skipping test on windows.");
}
const { actual } = await format("eslint", "index.js");
const expected = await getText("eslint", "index.result.js");
// Normalize these to account for CRLF issues on Windows
return assert.equal(actual.normalize(), expected.normalize());
});
});
57 changes: 39 additions & 18 deletions src/test/suite/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import * as assert from "assert";
import { readFile, rename } from "fs";
// tslint:disable-next-line: no-implicit-dependencies
import { Done } from "mocha";
import * as path from "path";
import * as prettier from "prettier";
import { promisify } from "util";
// tslint:disable-next-line: no-implicit-dependencies
import * as vscode from "vscode";

const readFileAsync: (
filePath: string,
encoding: "utf8"
) => Promise<string> = promisify(readFile);

/**
* gets the workspace folder by name
* @param name Workspace folder name
Expand All @@ -15,23 +24,36 @@ const getWorkspaceFolderUri = (workspaceFolderName: string) => {
return workspaceFolder!.uri;
};

export async function getText(workspaceFolderName: string, file: string) {
export async function getText(
workspaceFolderName: string,
expectedFile: string
) {
const base = getWorkspaceFolderUri(workspaceFolderName);
const absPath = path.join(base.fsPath, file);
const doc = await vscode.workspace.openTextDocument(absPath);
const text = doc.getText();
return text;
const expectedPath = path.join(base.fsPath, expectedFile);
const expected = await readFileAsync(expectedPath, "utf8");
return expected;
}

const prettierConfigOrig = path.resolve(__dirname, "../../../.prettierrc");
const prettierConfigTemp = path.resolve(__dirname, "../../../old.prettierrc");

export function moveRootPrettierRC(done: Done) {
rename(prettierConfigOrig, prettierConfigTemp, done);
}

export function putBackPrettierRC(done: Done) {
rename(prettierConfigTemp, prettierConfigOrig, done);
}

/**
* loads and format a file.
* @param file path relative to base URI (a workspaceFolder's URI)
* @param testFile path relative to base URI (a workspaceFolder's URI)
* @param base base URI
* @returns source code and resulting code
*/
export async function format(workspaceFolderName: string, file: string) {
export async function format(workspaceFolderName: string, testFile: string) {
const base = getWorkspaceFolderUri(workspaceFolderName);
const absPath = path.join(base.fsPath, file);
const absPath = path.join(base.fsPath, testFile);
const doc = await vscode.workspace.openTextDocument(absPath);
const text = doc.getText();
try {
Expand All @@ -42,14 +64,13 @@ export async function format(workspaceFolderName: string, file: string) {
throw error;
}
// tslint:disable-next-line: no-console
console.time(file);
return vscode.commands
.executeCommand("editor.action.formatDocument")
.then(() => {
// tslint:disable-next-line: no-console
console.timeEnd(file);
return { result: doc.getText(), source: text };
});
console.time(testFile);
await vscode.commands.executeCommand("editor.action.formatDocument");

// tslint:disable-next-line: no-console
console.timeEnd(testFile);

return { actual: doc.getText(), source: text };
}
/**
* Compare prettier's output (default settings)
Expand All @@ -67,9 +88,9 @@ async function formatSameAsPrettier(
filepath: file
}
};
const { result, source } = await format("project", file);
const { actual, source } = await format("project", file);
const prettierFormatted = prettier.format(source, prettierOptions);
assert.equal(result, prettierFormatted);
assert.equal(actual, prettierFormatted);
}

suite("Test format Document", function() {
Expand Down
12 changes: 6 additions & 6 deletions src/test/suite/ignore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import { format } from "./format.test";
suite("Test ignore", function() {
this.timeout(10000);
test("it does not format file", async () => {
const { result, source } = await format("project", "fileToIgnore.js");
assert.equal(result, source);
const { actual, source } = await format("project", "fileToIgnore.js");
assert.equal(actual, source);
});
/* cspell: disable-next-line */
test("it does not format subfolder/*", async () => {
const { result, source } = await format("project", "ignoreMe2/index.js");
assert.equal(result, source);
const { actual, source } = await format("project", "ignoreMe2/index.js");
assert.equal(actual, source);
});
/* cspell: disable-next-line */
test("it does not format sub-subfolder", async () => {
const { result, source } = await format(
const { actual, source } = await format(
"project",
/* cspell: disable-next-line */
"ignoreMe/subdir/index.js"
);
assert.equal(result, source);
assert.equal(actual, source);
});
});
29 changes: 9 additions & 20 deletions src/test/suite/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import * as assert from "assert";
import { format } from "./format.test";
import { platform } from "os";
import { format, getText } from "./format.test";

suite("Test plugins", function() {
this.timeout(10000);
test("it formats with plugins", async () => {
const { result } = await format("plugins", "index.php");
assert.equal(
result,
`<?php
array_map(
function ($arg1, $arg2) use ($var1, $var2) {
return $arg1 + $arg2 / ($var + $var2);
},
array(
"complex" => "code",
"with" => "inconsistent",
"formatting" => "is",
"hard" => "to",
"maintain" => true
)
);
`
);
if (platform() === "win32") {
return assert.ok(true, "Skipping test on windows.");
}
const { actual } = await format("plugins", "index.php");
const expected = await getText("plugins", "index.result.php");
// Normalize these to account for CRLF issues on Windows
assert.equal(actual.normalize(), expected.normalize());
});
});
27 changes: 16 additions & 11 deletions src/test/suite/tslint.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import * as assert from "assert";
import { format } from "./format.test";
import { platform } from "os";
import {
format,
getText,
moveRootPrettierRC,
putBackPrettierRC
} from "./format.test";

suite("Test tslint", function() {
this.timeout(10000);
this.timeout(60000);
this.beforeAll(moveRootPrettierRC);
this.afterAll(putBackPrettierRC);
test("it formats with prettier-tslint", async () => {
const { result } = await format("tslint", "withTslint.ts");
assert.equal(
result,
`// Settings (tslint): single-quote, trailing-comma, no-semi
function foo() {
return 'bar'
}
`
);
if (platform() === "win32") {
return assert.ok(true, "Skipping test on windows.");
}
const { actual } = await format("tslint", "index.ts");
const expected = await getText("tslint", "index.result.ts");
assert.equal(actual.normalize(), expected.normalize());
});
});
File renamed without changes.
4 changes: 4 additions & 0 deletions test-fixtures/eslint/index.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Settings (eslint): single-quote, trailing-comma, no-semi
function foo() {
return 'bar'
}
14 changes: 14 additions & 0 deletions test-fixtures/plugins/index.result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

array_map(
function ($arg1, $arg2) use ($var1, $var2) {
return $arg1 + $arg2 / ($var + $var2);
},
array(
"complex" => "code",
"with" => "inconsistent",
"formatting" => "is",
"hard" => "to",
"maintain" => true
)
);
2 changes: 1 addition & 1 deletion test-fixtures/plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "MIT",
"devDependencies": {
"@prettier/plugin-php": "^0.11.2",
"prettier": "^1.18.2"
"prettier": "^1.19.1"
},
"dependencies": {}
}
8 changes: 4 additions & 4 deletions test-fixtures/plugins/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ php-parser@glayzzle/php-parser#71485979b688d12fb130d3e853fdc00348671e00:
version "3.0.0-prerelease.8"
resolved "https://codeload.github.com/glayzzle/php-parser/tar.gz/71485979b688d12fb130d3e853fdc00348671e00"

prettier@^1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
prettier@^1.19.1:
version "1.19.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
4 changes: 4 additions & 0 deletions test-fixtures/tslint/index.result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Settings (tslint): single-quote, trailing-comma, no-semi
function foo() {
return 'bar'
}
File renamed without changes.
3 changes: 2 additions & 1 deletion test-fixtures/workspace/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"prettier.disableLanguages": [
"javascript"
],
"javascript.format.enable": true
"javascript.format.enable": true,
"files.eol": "\n"
}

0 comments on commit 0275b35

Please sign in to comment.