Permalink
Browse files

chore: Cleanup shared test utils package.

 - 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 c447f2f99978720f3e1b0143983207161557a571
Showing with 80 additions and 46 deletions.
  1. +1 −0 .travis.yml
  2. +1 −1 css-blocks.code-workspace
  3. +0 −3 packages/@css-blocks/build/README.md
  4. +0 −1 packages/@css-blocks/build/src/index.ts
  5. +0 −1 packages/@css-blocks/build/src/testing/index.ts
  6. +1 −0 packages/@css-blocks/core/src/Analyzer/ElementAnalysis.ts
  7. +1 −1 packages/@css-blocks/jsx/package.json
  8. +2 −1 packages/@css-blocks/jsx/test/analyzer/class-states-objstr-test.ts
  9. +1 −1 packages/@css-blocks/jsx/test/analyzer/dynamic-styles-test.ts
  10. +1 −1 packages/@css-blocks/jsx/test/analyzer/external-objstr-class-test.ts
  11. +2 −1 packages/@css-blocks/jsx/test/analyzer/external-objstr-root-styles-test.ts
  12. +2 −1 packages/@css-blocks/jsx/test/analyzer/inline-class-styles-test.ts
  13. +2 −1 packages/@css-blocks/jsx/test/analyzer/inline-objstr-styles-test.ts
  14. +2 −1 packages/@css-blocks/jsx/test/analyzer/inline-root-styles-test.ts
  15. +2 −1 packages/@css-blocks/jsx/test/analyzer/root-states-objstr-test.ts
  16. +2 −1 packages/@css-blocks/jsx/test/analyzer/states-attributes-test.ts
  17. +2 −1 packages/@css-blocks/jsx/test/analyzer/typed-file-analysis.ts
  18. +2 −1 packages/@css-blocks/jsx/test/blockImporter-test.ts
  19. +2 −1 packages/@css-blocks/jsx/test/parser-test.ts
  20. +2 −1 packages/@css-blocks/jsx/test/transformer/transformer-test.ts
  21. +28 −0 packages/@css-blocks/test-utils/README.md
  22. +3 −3 packages/@css-blocks/{build → test-utils}/package.json
  23. +1 −0 packages/@css-blocks/test-utils/src/index.ts
  24. +17 −20 packages/@css-blocks/{build/src/testing/transient-fs.ts → test-utils/src/mock-fs/index.ts}
  25. 0 packages/@css-blocks/{build → test-utils}/test/mocha.opts
  26. +3 −3 packages/@css-blocks/{build/test/transient-fs-test.ts → test-utils/test/mock-fs-test.ts}
  27. 0 packages/@css-blocks/{build → test-utils}/tsconfig.json
  28. 0 packages/@css-blocks/{build → test-utils}/tslint.cli.json
  29. 0 packages/@css-blocks/{build → test-utils}/tslint.json
  30. 0 packages/@css-blocks/{build → test-utils}/tslint.release.json
@@ -7,6 +7,7 @@ language: node_js
node_js:
- "8"
- "10"
- "11"

env:
- CXX=g++-4.8
@@ -35,7 +35,7 @@
"path": "packages/@css-blocks/ember-cli"
},
{
"path": "packages/@css-blocks/build"
"path": "packages/@css-blocks/test-utils"
}
],
"settings": {

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -851,6 +851,7 @@ function mapChoiceClasses(
configuration: ResolvedConfiguration,
map: Map<string, Style>,
includeAbsent: boolean,
/* tslint:disable-next-line */
...styles: Style[]
): AttributeValueChoice {
let choices = new Array<AttributeValueChoiceOption>();
@@ -40,6 +40,7 @@
"access": "public"
},
"devDependencies": {
"@css-blocks/test-utils": "^0.20.0-beta.4",
"@css-blocks/code-style": "^0.20.0-beta.4",
"@css-blocks/runtime": "^0.20.0-beta.4",
"babel-core": "^6.25.0",
@@ -50,7 +51,6 @@
"@css-blocks/runtime": "^0.18.0"
},
"dependencies": {
"@css-blocks/build": "^0.20.0-beta.4",
"@css-blocks/core": "^0.20.0-beta.4",
"@opticss/template-api": "^0.4.0",
"@opticss/util": "^0.4.0",
@@ -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 { suite, test } from "mocha-typescript";

@@ -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 { suite, test } from "mocha-typescript";
@@ -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 { suite, test } from "mocha-typescript";
@@ -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 { suite, test } from "mocha-typescript";

@@ -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 { suite, test } from "mocha-typescript";

@@ -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 { suite, test } from "mocha-typescript";

@@ -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 { suite, test } from "mocha-typescript";

@@ -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 { suite, test } from "mocha-typescript";

@@ -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 { skip, suite, test } from "mocha-typescript";

@@ -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 { skip, suite, test } from "mocha-typescript";

@@ -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 { assert } from "chai";
import { suite, test } from "mocha-typescript";
@@ -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 { suite, test } from "mocha-typescript";

@@ -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 c$$ from "@css-blocks/runtime";
import { TemplateIntegrationOptions } from "@opticss/template-api";
@@ -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();
```
@@ -1,9 +1,9 @@
{
"name": "@css-blocks/build",
"name": "@css-blocks/test-utils",
"private": true,
"version": "0.20.0-beta.4",
"description": "Build-time support for @css-blocks/*.",
"main": "dist/src/index.js",
"description": "Shared test utilities for @css-blocks/*.",
"main": "dist/src",
"scripts": {
"compile": "tsc --version && rm -rf dist && tsc",
"pretest": "yarn run compile",
@@ -0,0 +1 @@
export * from "./mock-fs";
@@ -6,14 +6,16 @@ export interface FileContent {
[filename: string]: string | FileContent;
}

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

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

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

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

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

// tslint:disable-next-line no-default-export
export default transient;
mock.fileCount = function fileCount(): number {
return FILES.length;
};
@@ -3,9 +3,9 @@ import * as fs from "fs";
import { suite, test } from "mocha-typescript";
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 {
@test "can write and clean up files"() {
mock({
@@ -40,7 +40,7 @@ export class TransientFsTest {
assert.isFalse(fs.existsSync("top-file.txt"));
}
@test "can restore no files"() {
assert.equal(mock._files.length, 0);
assert.equal(mock.fileCount(), 0);
mock.restore();
}
}

0 comments on commit c447f2f

Please sign in to comment.