Skip to content

Commit

Permalink
chore: Cleanup shared test utils package.
Browse files Browse the repository at this point in the history
 - Move @css-blocks/build => @css-blocks/test-utils.
 - Move @css-blocks/test-utils to devDependencies in JSX integration.
 - Clean up exports API of test-utils so we don't need deep requires.
 - Clean up typings of mock function in test-utils so we don't need Object.apply.
 - Add README for test-utils.
 - Rename transient-fs to mock-fs.
 - Move _files and _directories private properties to truly private module scoped contants.
  • Loading branch information
amiller-gh committed Dec 6, 2018
1 parent 41bdd29 commit c447f2f
Show file tree
Hide file tree
Showing 30 changed files with 80 additions and 46 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ language: node_js
node_js: node_js:
- "8" - "8"
- "10" - "10"
- "11"


env: env:
- CXX=g++-4.8 - CXX=g++-4.8
Expand Down
2 changes: 1 addition & 1 deletion css-blocks.code-workspace
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"path": "packages/@css-blocks/ember-cli" "path": "packages/@css-blocks/ember-cli"
}, },
{ {
"path": "packages/@css-blocks/build" "path": "packages/@css-blocks/test-utils"
} }
], ],
"settings": { "settings": {
Expand Down
3 changes: 0 additions & 3 deletions packages/@css-blocks/build/README.md

This file was deleted.

1 change: 0 additions & 1 deletion packages/@css-blocks/build/src/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/@css-blocks/build/src/testing/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/@css-blocks/core/src/Analyzer/ElementAnalysis.ts
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ function mapChoiceClasses(
configuration: ResolvedConfiguration, configuration: ResolvedConfiguration,
map: Map<string, Style>, map: Map<string, Style>,
includeAbsent: boolean, includeAbsent: boolean,
/* tslint:disable-next-line */
...styles: Style[] ...styles: Style[]
): AttributeValueChoice { ): AttributeValueChoice {
let choices = new Array<AttributeValueChoiceOption>(); let choices = new Array<AttributeValueChoiceOption>();
Expand Down
2 changes: 1 addition & 1 deletion packages/@css-blocks/jsx/package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"access": "public" "access": "public"
}, },
"devDependencies": { "devDependencies": {
"@css-blocks/test-utils": "^0.20.0-beta.4",
"@css-blocks/code-style": "^0.20.0-beta.4", "@css-blocks/code-style": "^0.20.0-beta.4",
"@css-blocks/runtime": "^0.20.0-beta.4", "@css-blocks/runtime": "^0.20.0-beta.4",
"babel-core": "^6.25.0", "babel-core": "^6.25.0",
Expand All @@ -50,7 +51,6 @@
"@css-blocks/runtime": "^0.18.0" "@css-blocks/runtime": "^0.18.0"
}, },
"dependencies": { "dependencies": {
"@css-blocks/build": "^0.20.0-beta.4",
"@css-blocks/core": "^0.20.0-beta.4", "@css-blocks/core": "^0.20.0-beta.4",
"@opticss/template-api": "^0.4.0", "@opticss/template-api": "^0.4.0",
"@opticss/util": "^0.4.0", "@opticss/util": "^0.4.0",
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";


import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";


import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { skip, suite, test } from "mocha-typescript"; import { skip, suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { skip, suite, test } from "mocha-typescript"; import { skip, suite, test } from "mocha-typescript";


Expand Down
3 changes: 2 additions & 1 deletion packages/@css-blocks/jsx/test/blockImporter-test.ts
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { Block } from "@css-blocks/core"; import { Block } from "@css-blocks/core";
import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";
Expand Down
3 changes: 2 additions & 1 deletion packages/@css-blocks/jsx/test/parser-test.ts
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { assert } from "chai"; import { assert } from "chai";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";


Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
import mock from "@css-blocks/build/dist/src/testing/transient-fs"; import { mock } from "@css-blocks/test-utils";

import { Analysis, BlockCompiler, Options as CSSBlocksOptions, StyleMapping, resolveConfiguration as resolveBlocksConfiguration } from "@css-blocks/core"; import { Analysis, BlockCompiler, Options as CSSBlocksOptions, StyleMapping, resolveConfiguration as resolveBlocksConfiguration } from "@css-blocks/core";
import c$$ from "@css-blocks/runtime"; import c$$ from "@css-blocks/runtime";
import { TemplateIntegrationOptions } from "@opticss/template-api"; import { TemplateIntegrationOptions } from "@opticss/template-api";
Expand Down
28 changes: 28 additions & 0 deletions packages/@css-blocks/test-utils/README.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,28 @@
# Shared Test Utilities for `@css-blocks` Packages

These utilities are only used in the @css-blocks tests, so this package is not published.

## `import { mock } from @css-blocks/test-utils`

The `mock` utility provides tests the ability to temporarily mocks files on the filesystem. Works for all supported versions of Node.js. Once mocked, files generated using `mock` can be accessed like regular files by all Node.js filesystem calls.

You can mock files using the following API:

```js
mock({
'filename.js': 'file-contents',
'foldernames': {
'can': {
'nest': {
'file.css': 'file-contents'
}
}
}
});
```

To clean up all mocked filesystem files and directories when done, simply call:

```js
mock.restore();
```
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,9 @@
{ {
"name": "@css-blocks/build", "name": "@css-blocks/test-utils",
"private": true, "private": true,
"version": "0.20.0-beta.4", "version": "0.20.0-beta.4",
"description": "Build-time support for @css-blocks/*.", "description": "Shared test utilities for @css-blocks/*.",
"main": "dist/src/index.js", "main": "dist/src",
"scripts": { "scripts": {
"compile": "tsc --version && rm -rf dist && tsc", "compile": "tsc --version && rm -rf dist && tsc",
"pretest": "yarn run compile", "pretest": "yarn run compile",
Expand Down
1 change: 1 addition & 0 deletions packages/@css-blocks/test-utils/src/index.ts
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./mock-fs";
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ export interface FileContent {
[filename: string]: string | FileContent; [filename: string]: string | FileContent;
} }


export interface TransientFiles { export interface FileMocker {
(filesObj: FileContent): void; (filesObj: FileContent): void;
restore: () => void; restore: () => void;
_files: string[]; fileCount: () => number;
_directories: string[];
} }


function transientFS(filesObj: FileContent) { const FILES: string[] = [];
const DIRS: string[] = [];

export const mock = <FileMocker>function mock(filesObj: FileContent) {
let keys = Object.keys(filesObj); let keys = Object.keys(filesObj);
for (let key of keys) { for (let key of keys) {
let content = filesObj[key]; let content = filesObj[key];
Expand All @@ -23,41 +25,36 @@ function transientFS(filesObj: FileContent) {
if (!existsSync(key)) { if (!existsSync(key)) {
mkdirSync(key); mkdirSync(key);
let fullPath = path.resolve(key); let fullPath = path.resolve(key);
transient._directories.push(fullPath); DIRS.push(fullPath);
} }
process.chdir(key); process.chdir(key);
transientFS(content); mock(content);
} }
finally { finally {
process.chdir(dir); process.chdir(dir);
} }
} else { } else {
let fullPath = path.resolve(key); let fullPath = path.resolve(key);
writeFileSync(fullPath, content); writeFileSync(fullPath, content);
transient._files.push(fullPath); FILES.push(fullPath);
} }
} }
} };


function restore() { mock.restore = function restore() {
let file, dir; let file, dir;
while (file = transient._files.pop()) { while (file = FILES.pop()) {
if (existsSync(file)) { if (existsSync(file)) {
unlinkSync(file); unlinkSync(file);
} }
} }
while (dir = transient._directories.pop()) { while (dir = DIRS.pop()) {
if (existsSync(dir)) { if (existsSync(dir)) {
rmdirSync(dir); rmdirSync(dir);
} }
} }
} };

const transient: TransientFiles = Object.assign(transientFS, {
_files: [],
_directories: [],
restore,
});


// tslint:disable-next-line no-default-export mock.fileCount = function fileCount(): number {
export default transient; return FILES.length;
};
File renamed without changes.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import * as fs from "fs";
import { suite, test } from "mocha-typescript"; import { suite, test } from "mocha-typescript";
import * as path from "path"; import * as path from "path";


import mock from "../src/testing/transient-fs"; import { mock } from "../src/mock-fs";


@suite("transient-fs") @suite("mock-fs")
export class TransientFsTest { export class TransientFsTest {
@test "can write and clean up files"() { @test "can write and clean up files"() {
mock({ mock({
Expand Down Expand Up @@ -40,7 +40,7 @@ export class TransientFsTest {
assert.isFalse(fs.existsSync("top-file.txt")); assert.isFalse(fs.existsSync("top-file.txt"));
} }
@test "can restore no files"() { @test "can restore no files"() {
assert.equal(mock._files.length, 0); assert.equal(mock.fileCount(), 0);
mock.restore(); mock.restore();
} }
} }
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit c447f2f

Please sign in to comment.