Skip to content

Commit

Permalink
fix: Glob stays in the tree's root path - #88
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonJang committed Mar 10, 2020
1 parent 9dec24a commit 3795d40
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index/generateTrees.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import chalk from "chalk";

import logger from "../shared/logger";
Expand All @@ -9,6 +10,8 @@ import { toFractalTree } from "./generateTrees/toFractalTree";
import { detectLonelyFiles } from "./shared/detect-lonely-files";
import { Config } from "../index";

const getRootFolder = (parentDir: string) => parentDir.split(path.sep).pop()

export function generateTrees(
restructureMap: { [key: string]: string[] },
{ avoidSingleFile }: Config
Expand All @@ -34,7 +37,7 @@ export function generateTrees(
filePath => !usedFilePaths.has(filePath)
);

logger.log(chalk.bold.blue(rootPath));
logger.log(chalk.bold.blue(getRootFolder(parentFolder)));
printTree(Object.values(tree));
if (unusedFiles.length > 0) {
logger.warn(
Expand Down
34 changes: 34 additions & 0 deletions tests/end-to-end.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs-extra";
import glob from "glob";
import path from "path";
import treeDir from "tree-node-cli";
import chalk from 'chalk';

import { buildGraph } from "../src/index/generateTrees/buildGraph";
import { run } from "../src/index";
Expand Down Expand Up @@ -37,7 +38,27 @@ const treeDirWithContents = (dir: string) => {
return tree;
};

const mocks = {
log: jest.spyOn(console, "log").mockImplementationOnce(() => {}),
error: jest.spyOn(console, "error").mockImplementationOnce(() => {}),
info: jest.spyOn(console, "info").mockImplementationOnce(() => {}),
warn: jest.spyOn(console, "warn").mockImplementationOnce(() => {}),
exit: jest
.spyOn(process, "exit")
// @ts-ignore - eslint won't allow assertion of `code as never`
.mockImplementationOnce(code => code),
};

describe("end-to-end", () => {
beforeEach(() => {
process.env.NODE_ENV = "production";
});

afterEach(() => {
jest.resetAllMocks();
jest.resetModules();
});

const fixturePath = path.join(__dirname, "fixtures");
const testCases = fs.readdirSync(fixturePath);

Expand All @@ -52,7 +73,9 @@ describe("end-to-end", () => {

await run(["--write", path.join(tmpPath, rootPath)]);
buildGraph(glob.sync(path.join(copyPath, "/**/*.*")));

expect(treeDir(path.join(tmpPath, rootPath))).toMatchSnapshot();
expect(mocks.log).toBeCalledWith(chalk.bold.blue(rootPath.split(path.sep).pop()));

const treeContents = treeDirWithContents(copyPath);
Object.keys(treeContents).forEach(k => {
Expand All @@ -63,6 +86,15 @@ describe("end-to-end", () => {
});

describe("end-to-end --avoid-single-file", () => {
beforeEach(() => {
process.env.NODE_ENV = "production";
});

afterEach(() => {
jest.resetAllMocks();
jest.resetModules();
});

const fixturePath = path.join(__dirname, "fixtures");
const testCases = fs.readdirSync(fixturePath);

Expand All @@ -81,7 +113,9 @@ describe("end-to-end --avoid-single-file", () => {
path.join(tmpPath, rootPath),
]);
buildGraph(glob.sync(path.join(copyPath, "/**/*.*")));

expect(treeDir(path.join(tmpPath, rootPath))).toMatchSnapshot();
expect(mocks.log).toBeCalledWith(chalk.bold.blue(rootPath.split(path.sep).pop()));

const treeContents = treeDirWithContents(copyPath);
Object.keys(treeContents).forEach(k => {
Expand Down

0 comments on commit 3795d40

Please sign in to comment.