Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed May 25, 2018
1 parent af0b8ba commit 8024032
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/fs-tree-utils/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -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> | 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
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
},
},
}
Expand Down
25 changes: 23 additions & 2 deletions packages/typescript/fs-tree-utils/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 = {
Expand All @@ -55,6 +59,8 @@ describe('create function', () => {
const firstBornFolders = {
emptyFoo: {},
emptyBar: {},
emptyBaz: new Directory(),
emptyQux: new Directory({}),
firstBornFiles
}

Expand All @@ -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({
Expand Down

0 comments on commit 8024032

Please sign in to comment.