From 802403212c1a4a5f76e5596a3dc91118514efe00 Mon Sep 17 00:00:00 2001 From: khai96_ Date: Fri, 25 May 2018 17:34:07 +0700 Subject: [PATCH] Add test cases --- .../lib/classes/fs-representation/index.ts | 4 +- .../fs-tree-utils/lib/types/index.ts | 4 +- .../test/__snapshots__/index.test.ts.snap | 81 +++++++++++++++++++ .../fs-tree-utils/test/index.test.ts | 25 +++++- 4 files changed, 108 insertions(+), 6 deletions(-) diff --git a/packages/typescript/fs-tree-utils/lib/classes/fs-representation/index.ts b/packages/typescript/fs-tree-utils/lib/classes/fs-representation/index.ts index 741d67d9..6abf5391 100644 --- a/packages/typescript/fs-tree-utils/lib/classes/fs-representation/index.ts +++ b/packages/typescript/fs-tree-utils/lib/classes/fs-representation/index.ts @@ -28,9 +28,9 @@ export namespace FileSystemRepresentation { export class Directory extends FileSystemRepresentation { readonly content: Directory.Content - constructor (content: Directory.Content) { + constructor (content?: Directory.Content | null) { super() - this.content = content + this.content = content || {} } async write (dirname: string) { diff --git a/packages/typescript/fs-tree-utils/lib/types/index.ts b/packages/typescript/fs-tree-utils/lib/types/index.ts index 818cb556..78f2e99b 100644 --- a/packages/typescript/fs-tree-utils/lib/types/index.ts +++ b/packages/typescript/fs-tree-utils/lib/types/index.ts @@ -1,12 +1,12 @@ import FileSystemRepresentation from '../classes/fs-representation' export type FileContent = string -export type TreeObject = {[name: string]: TreeObject | FileContent} +export type TreeObject = {[name: string]: TreeNode} export type TreeNode = FileContent | TreeObject export type Tree = TreeNode export type WriteFileContent = FileContent | Buffer export type WriteFunction = (name: string) => Promise | void -export type WriteTreeObject = {readonly [name: string]: WriteTreeObject | WriteFileContent} +export type WriteTreeObject = {readonly [name: string]: WriteTreeNode} export type WriteTreeNode = WriteFileContent | WriteFunction | WriteTreeObject | FileSystemRepresentation export type WriteTree = WriteTreeNode diff --git a/packages/typescript/fs-tree-utils/test/__snapshots__/index.test.ts.snap b/packages/typescript/fs-tree-utils/test/__snapshots__/index.test.ts.snap index e98eed7c..a3b366bb 100644 --- a/packages/typescript/fs-tree-utils/test/__snapshots__/index.test.ts.snap +++ b/packages/typescript/fs-tree-utils/test/__snapshots__/index.test.ts.snap @@ -96,51 +96,78 @@ Object { "flat": Object { "all": Array [ "tmp/create.1/bar", + "tmp/create.1/baz", "tmp/create.1/emptyBar", + "tmp/create.1/emptyBaz", "tmp/create.1/emptyFoo", + "tmp/create.1/emptyQux", "tmp/create.1/firstBornFiles", "tmp/create.1/firstBornFiles/bar", + "tmp/create.1/firstBornFiles/baz", "tmp/create.1/firstBornFiles/foo", + "tmp/create.1/firstBornFiles/qux", "tmp/create.1/foo", + "tmp/create.1/qux", ], "directories": Array [ "tmp/create.1/emptyBar", + "tmp/create.1/emptyBaz", "tmp/create.1/emptyFoo", + "tmp/create.1/emptyQux", "tmp/create.1/firstBornFiles", ], "fileContents": Object { "tmp/create.1/bar": "bar", + "tmp/create.1/baz": "baz", "tmp/create.1/firstBornFiles/bar": "bar", + "tmp/create.1/firstBornFiles/baz": "baz", "tmp/create.1/firstBornFiles/foo": "foo", + "tmp/create.1/firstBornFiles/qux": "qux", "tmp/create.1/foo": "foo", + "tmp/create.1/qux": "qux", }, "files": Array [ "tmp/create.1/bar", + "tmp/create.1/baz", "tmp/create.1/firstBornFiles/bar", + "tmp/create.1/firstBornFiles/baz", "tmp/create.1/firstBornFiles/foo", + "tmp/create.1/firstBornFiles/qux", "tmp/create.1/foo", + "tmp/create.1/qux", ], }, "nested": Object { "bar": "bar", + "baz": "baz", "emptyBar": Object {}, + "emptyBaz": Object {}, "emptyFoo": Object {}, + "emptyQux": Object {}, "firstBornFiles": Object { "bar": "bar", + "baz": "baz", "foo": "foo", + "qux": "qux", }, "foo": "foo", + "qux": "qux", }, }, "secondBorn": Object { "flat": Object { "all": Array [ "tmp/create.1/bar", + "tmp/create.1/baz", "tmp/create.1/emptyBar", + "tmp/create.1/emptyBaz", "tmp/create.1/emptyFoo", + "tmp/create.1/emptyQux", "tmp/create.1/firstBornFiles", "tmp/create.1/firstBornFiles/bar", + "tmp/create.1/firstBornFiles/baz", "tmp/create.1/firstBornFiles/foo", + "tmp/create.1/firstBornFiles/qux", "tmp/create.1/foo", "tmp/create.1/newFileA", "tmp/create.1/newFileB", @@ -154,10 +181,23 @@ Object { "tmp/create.1/newTreeC/emptyFile/file", "tmp/create.1/newTreeC/nonEmptyFile", "tmp/create.1/newTreeC/nonEmptyFile/file", + "tmp/create.1/newTreeD", + "tmp/create.1/newTreeD/newTreeA", + "tmp/create.1/newTreeD/newTreeB", + "tmp/create.1/newTreeD/newTreeB/empty", + "tmp/create.1/newTreeD/newTreeC", + "tmp/create.1/newTreeD/newTreeC/empty", + "tmp/create.1/newTreeD/newTreeC/emptyFile", + "tmp/create.1/newTreeD/newTreeC/emptyFile/file", + "tmp/create.1/newTreeD/newTreeC/nonEmptyFile", + "tmp/create.1/newTreeD/newTreeC/nonEmptyFile/file", + "tmp/create.1/qux", ], "directories": Array [ "tmp/create.1/emptyBar", + "tmp/create.1/emptyBaz", "tmp/create.1/emptyFoo", + "tmp/create.1/emptyQux", "tmp/create.1/firstBornFiles", "tmp/create.1/newTreeA", "tmp/create.1/newTreeB", @@ -166,37 +206,62 @@ Object { "tmp/create.1/newTreeC/empty", "tmp/create.1/newTreeC/emptyFile", "tmp/create.1/newTreeC/nonEmptyFile", + "tmp/create.1/newTreeD", + "tmp/create.1/newTreeD/newTreeA", + "tmp/create.1/newTreeD/newTreeB", + "tmp/create.1/newTreeD/newTreeB/empty", + "tmp/create.1/newTreeD/newTreeC", + "tmp/create.1/newTreeD/newTreeC/empty", + "tmp/create.1/newTreeD/newTreeC/emptyFile", + "tmp/create.1/newTreeD/newTreeC/nonEmptyFile", ], "fileContents": Object { "tmp/create.1/bar": "bar", + "tmp/create.1/baz": "baz", "tmp/create.1/firstBornFiles/bar": "bar", + "tmp/create.1/firstBornFiles/baz": "baz", "tmp/create.1/firstBornFiles/foo": "foo", + "tmp/create.1/firstBornFiles/qux": "qux", "tmp/create.1/foo": "foo", "tmp/create.1/newFileA": "First New File", "tmp/create.1/newFileB": "Second New File", "tmp/create.1/newFileC": "Third New File", "tmp/create.1/newTreeC/emptyFile/file": "", "tmp/create.1/newTreeC/nonEmptyFile/file": "Not empty", + "tmp/create.1/newTreeD/newTreeC/emptyFile/file": "", + "tmp/create.1/newTreeD/newTreeC/nonEmptyFile/file": "Not empty", + "tmp/create.1/qux": "qux", }, "files": Array [ "tmp/create.1/bar", + "tmp/create.1/baz", "tmp/create.1/firstBornFiles/bar", + "tmp/create.1/firstBornFiles/baz", "tmp/create.1/firstBornFiles/foo", + "tmp/create.1/firstBornFiles/qux", "tmp/create.1/foo", "tmp/create.1/newFileA", "tmp/create.1/newFileB", "tmp/create.1/newFileC", "tmp/create.1/newTreeC/emptyFile/file", "tmp/create.1/newTreeC/nonEmptyFile/file", + "tmp/create.1/newTreeD/newTreeC/emptyFile/file", + "tmp/create.1/newTreeD/newTreeC/nonEmptyFile/file", + "tmp/create.1/qux", ], }, "nested": Object { "bar": "bar", + "baz": "baz", "emptyBar": Object {}, + "emptyBaz": Object {}, "emptyFoo": Object {}, + "emptyQux": Object {}, "firstBornFiles": Object { "bar": "bar", + "baz": "baz", "foo": "foo", + "qux": "qux", }, "foo": "foo", "newFileA": "First New File", @@ -215,6 +280,22 @@ Object { "file": "Not empty", }, }, + "newTreeD": Object { + "newTreeA": Object {}, + "newTreeB": Object { + "empty": Object {}, + }, + "newTreeC": Object { + "empty": Object {}, + "emptyFile": Object { + "file": "", + }, + "nonEmptyFile": Object { + "file": "Not empty", + }, + }, + }, + "qux": "qux", }, }, } diff --git a/packages/typescript/fs-tree-utils/test/index.test.ts b/packages/typescript/fs-tree-utils/test/index.test.ts index 1d72860a..144a7e5c 100644 --- a/packages/typescript/fs-tree-utils/test/index.test.ts +++ b/packages/typescript/fs-tree-utils/test/index.test.ts @@ -8,6 +8,8 @@ const tree = require('./data/tree.yaml') const oldCwd = cwd() const tmpContainer = tempPath('fs-tree-utils.') const tmp = 'tmp' +const {FileSystemRepresentation} = subject +const {File, Directory} = FileSystemRepresentation const createTreeGetter = (container: string) => () => Promise.all([ subject.read.nested(container), @@ -43,7 +45,9 @@ describe('create function', () => { const firstBornFiles = { foo: 'foo', - bar: 'bar' + bar: 'bar', + baz: new File('baz'), + qux: new File(new Buffer('qux')) } const secondBornFiles = { @@ -55,6 +59,8 @@ describe('create function', () => { const firstBornFolders = { emptyFoo: {}, emptyBar: {}, + emptyBaz: new Directory(), + emptyQux: new Directory({}), firstBornFiles } @@ -71,7 +77,22 @@ describe('create function', () => { nonEmptyFile: { file: 'Not empty' } - } + }, + newTreeD: new Directory({ + newTreeA: {}, + newTreeB: new Directory({ + empty: {} + }), + newTreeC: { + empty: {}, + emptyFile: new Directory({ + file: '' + }), + nonEmptyFile: { + file: 'Not empty' + } + } + }) } await subject.create({