added tests for Smartfile instance

This commit is contained in:
Philipp Kunz 2017-04-30 18:13:17 +02:00
parent 9d02fccc01
commit 4899d454eb
6 changed files with 172 additions and 60 deletions

View File

@ -3,7 +3,7 @@ export interface ISmartfileConstructorOptions {
path?: string; path?: string;
contentString?: string; contentString?: string;
contentBuffer?: Buffer; contentBuffer?: Buffer;
cwd?: string; base?: string;
} }
/** /**
* class Smartfile * class Smartfile
@ -14,10 +14,6 @@ export declare class Smartfile {
* the full path of the file on disk * the full path of the file on disk
*/ */
path: string; path: string;
/**
* gulp-compatibility: alias of this.contentBuffer
*/
contents: Buffer;
/** /**
* the content of the file as Buffer * the content of the file as Buffer
*/ */
@ -25,7 +21,7 @@ export declare class Smartfile {
/** /**
* The current working directory of the file * The current working directory of the file
*/ */
cwd: string; base: string;
/** /**
* sync the file with disk * sync the file with disk
*/ */
@ -35,11 +31,6 @@ export declare class Smartfile {
* @param optionsArg * @param optionsArg
*/ */
constructor(optionsArg: ISmartfileConstructorOptions); constructor(optionsArg: ISmartfileConstructorOptions);
/**
* return relative path of file
* ->
*/
readonly relative: string;
/** /**
* set contents from string * set contents from string
* @param contentString * @param contentString
@ -53,4 +44,26 @@ export declare class Smartfile {
* read file from disk * read file from disk
*/ */
read(): Promise<void>; read(): Promise<void>;
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
contents: Buffer;
/**
* vinyl-compatibility
*/
readonly cwd: string;
/**
* return relative path of file
*/
readonly relative: string;
/**
* return truw when the file has content
*/
isNull(): boolean;
/**
* return true if contents are Buffer
*/
isBuffer(): boolean;
isDirectory(): boolean;
isStream(): boolean;
} }

View File

@ -20,24 +20,15 @@ class Smartfile {
constructor(optionsArg) { constructor(optionsArg) {
if (optionsArg.contentBuffer) { if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer; this.contentBuffer = optionsArg.contentBuffer;
this.contents = optionsArg.contentBuffer;
} }
else if (optionsArg.contentString) { else if (optionsArg.contentString) {
this.contentBuffer = optionsArg.contentBuffer; this.setContentsFromString(optionsArg.contentString);
this.contents = Buffer.from(optionsArg.contentString);
} }
else { else {
console.log('created empty Smartfile?'); console.log('created empty Smartfile?');
} }
this.path = optionsArg.path; this.path = optionsArg.path;
this.cwd = optionsArg.cwd; this.base = optionsArg.base;
}
/**
* return relative path of file
* ->
*/
get relative() {
return '';
} }
/** /**
* set contents from string * set contents from string
@ -60,6 +51,54 @@ class Smartfile {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
}); });
} }
// -----------------------------------------------
// vinyl compatibility
// -----------------------------------------------
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
get contents() {
return this.contentBuffer;
}
set contents(contentsArg) {
this.contentBuffer = contentsArg;
}
/**
* vinyl-compatibility
*/
get cwd() {
return this.base;
}
/**
* return relative path of file
*/
get relative() {
return this.path;
}
/**
* return truw when the file has content
*/
isNull() {
if (!this.contentBuffer) {
return true;
}
return false;
}
/**
* return true if contents are Buffer
*/
isBuffer() {
if (this.contents instanceof Buffer) {
return true;
}
return false;
}
isDirectory() {
return false;
}
isStream() {
return false;
}
} }
exports.Smartfile = Smartfile; exports.Smartfile = Smartfile;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRmaWxlLmNsYXNzZXMuc21hcnRmaWxlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRmaWxlLmNsYXNzZXMuc21hcnRmaWxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7QUFTQTs7O0dBR0c7QUFDSDtJQTBCRTs7O09BR0c7SUFDSCxZQUFhLFVBQXdDO1FBQ25ELEVBQUUsQ0FBQyxDQUFDLFVBQVUsQ0FBQyxhQUFhLENBQUMsQ0FBQyxDQUFDO1lBQzdCLElBQUksQ0FBQyxhQUFhLEdBQUcsVUFBVSxDQUFDLGFBQWEsQ0FBQTtZQUM3QyxJQUFJLENB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M0IsQ0FBQztJQUNELElBQUksUUFBUSxDQUFFLFdBQVc7UUFDdkIsSUFBSSxDQUFDLGFBQWEsR0FBRyxXQUFXLENBQUE7SUFDbEMsQ0FBQztJQUVEOztPQUVHO0lBQ0gsSUFBSSxHQUFHO1FBQ0wsTUFBTSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUE7SUFDbEIsQ0FBQztJQUVEOztPQUVHO0lBQ0gsSUFBSSxRQUFRO1FBQ1YsTUFBTSxDQUFDLElBQUksQ0FBQyxJQUFJLENBQUE7SUFDbEIsQ0FBQztJQUVEOztPQUVHO0lBQ0gsTUFBTTtRQUNKLEVBQUUsQ0FBQyxDQUFDLENBQUMsSUFBSSxDQUFDLGFBQWEsQ0FBQyxDQUFDLENBQUM7WUFDeEIsTUFBTSxDQUFDLElBQUksQ0FBQTtRQUNiLENBQUM7UUFDRCxNQUFNLENBQUMsS0FBSyxDQUFBO0lBQ2QsQ0FBQztJQUVEOztPQUVHO0lBQ0gsUUFBUTtRQUNOLEVBQUUsQ0FBQyxDQUFDLElBQUksQ0FBQyxRQUFRLFlBQVksTUFBTSxDQUFDLENBQUMsQ0FBQztZQUNwQyxNQUFNLENBQUMsSUFBSSxDQUFBO1FBQ2IsQ0FBQztRQUNELE1BQU0sQ0FBQyxLQUFLLENBQUE7SUFDZCxDQUFDO0lBRUQsV0FBVztRQUNULE1BQU0sQ0FBQyxLQUFLLENBQUE7SUFDZCxDQUFDO0lBRUQsUUFBUTtRQUNOLE1BQU0sQ0FBQyxLQUFLLENBQUE7SUFDZCxDQUFDO0NBQ0Y7QUFqSEQsOEJBaUhDIn0=

File diff suppressed because one or more lines are too long

View File

@ -51,7 +51,7 @@ tap.test('.fs.listFileTree() -> should get a file tree', async () => {
expect(folderArrayArg).to.not.deep.include('mytest.json') expect(folderArrayArg).to.not.deep.include('mytest.json')
}) })
tap.test('.fstoObjectFromFileTree -> should read a file tree into an Object', async () => { tap.test('.fs.toObjectFromFileTree -> should read a file tree into an Object', async () => {
let fileArrayArg = await smartfile.fs.fileTreeToObject(path.resolve('./test/'), '**/*.txt') let fileArrayArg = await smartfile.fs.fileTreeToObject(path.resolve('./test/'), '**/*.txt')
// expect(fileArrayArg[1]).to.be.instanceof(smartfile.Smartfile) // expect(fileArrayArg[1]).to.be.instanceof(smartfile.Smartfile)
}) })
@ -72,36 +72,28 @@ tap.test('.fs.remove() -> should remove an entire directory', async () => {
}) })
tap.test('smartfile.fs.remove -> should remove single files', async () => { tap.test('.fs.remove -> should remove single files', async () => {
await expect(smartfile.fs.remove('./test/temp/mytestRenamed.yaml')).to.eventually.be.fulfilled await expect(smartfile.fs.remove('./test/temp/mytestRenamed.yaml')).to.eventually.be.fulfilled
}) })
tap.test('smartfile.fs.removeSync -> should remove single files synchronouly', async () => { tap.test('.fs.removeSync -> should remove single files synchronouly', async () => {
smartfile.fs.removeSync('./test/temp/testfile1.txt') smartfile.fs.removeSync('./test/temp/testfile1.txt')
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
}) })
tap.test('smartfile.fs.removeMany -> should remove and array of files', async () => { tap.test('.fs.removeMany -> should remove and array of files', async () => {
smartfile.fs.removeMany([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ]).then(() => { smartfile.fs.removeMany([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ]).then(() => {
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
}) })
}) })
tap.test('smartfile.fs.removeManySync -> should remove and array of single files synchronouly', async () => { tap.test('.fs.removeManySync -> should remove and array of single files synchronouly', async () => {
smartfile.fs.removeManySync([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ]) smartfile.fs.removeManySync([ './test/temp/testfile1.txt', './test/temp/testfile2.txt' ])
expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false expect(smartfile.fs.fileExistsSync('./test/temp/testfile1.txt')).to.be.false
expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false expect(smartfile.fs.fileExistsSync('./test/temp/testfile2.txt')).to.be.false
}) })
// ---------------------------
// smartfile.interpreter
// ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json')
})
tap.test('.fs.toObjectSync() -> should read an ' + '.yaml' + ' file to an object', async () => { tap.test('.fs.toObjectSync() -> should read an ' + '.yaml' + ' file to an object', async () => {
let testData = smartfile.fs.toObjectSync('./test/mytest.yaml') let testData = smartfile.fs.toObjectSync('./test/mytest.yaml')
expect(testData).have.property('key1', 'this works') expect(testData).have.property('key1', 'this works')
@ -129,6 +121,18 @@ tap.test('.fs.toVinylSync -> should read an ' + '.json OR .yaml' + ' file to an
expect(vinyl.isVinyl(testData)).to.be.true expect(vinyl.isVinyl(testData)).to.be.true
}) })
// ---------------------------
// smartfile.interpreter
// ---------------------------
tap.test('.interpreter.filetype() -> should get the file type from a string', async () => {
expect(smartfile.interpreter.filetype('./somefolder/data.json')).equal('json')
})
// ---------------------------
// smartfile.memory
// ---------------------------
tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => { tap.test('.memory.toVinylFileSync() -> should produce a vinylFile', async () => {
let localString = 'myString' let localString = 'myString'
let localOptions = { filename: 'vinylfile2', base: '/someDir' } let localOptions = { filename: 'vinylfile2', base: '/someDir' }
@ -183,4 +187,18 @@ tap.test('.remote.toString() -> should reject a Promise when the link is false',
.to.eventually.be.rejected .to.eventually.be.rejected
}) })
// ---------------------------
// smartfile.Smartfile
// ---------------------------
tap.test('.Smartfile -> should produce vinyl compatible files', async () => {
let smartfileArray = await smartfile.fs.fileTreeToObject(process.cwd(), './test/testfolder/**/*')
let localSmartfile = smartfileArray[0]
expect(localSmartfile).to.be.instanceof(smartfile.Smartfile)
expect(localSmartfile.contents).to.be.instanceof(Buffer)
expect(localSmartfile.isBuffer()).to.be.true
expect(localSmartfile.isDirectory()).to.be.false
expect(localSmartfile.isNull()).to.be.false
})
tap.start() tap.start()

View File

@ -4,7 +4,7 @@ export interface ISmartfileConstructorOptions {
path?: string path?: string
contentString?: string contentString?: string
contentBuffer?: Buffer contentBuffer?: Buffer
cwd?: string base?: string
} }
/** /**
@ -17,11 +17,6 @@ export class Smartfile {
*/ */
path: string path: string
/**
* gulp-compatibility: alias of this.contentBuffer
*/
contents: Buffer
/** /**
* the content of the file as Buffer * the content of the file as Buffer
*/ */
@ -30,7 +25,7 @@ export class Smartfile {
/** /**
* The current working directory of the file * The current working directory of the file
*/ */
cwd: string base: string
/** /**
* sync the file with disk * sync the file with disk
@ -44,23 +39,13 @@ export class Smartfile {
constructor (optionsArg: ISmartfileConstructorOptions) { constructor (optionsArg: ISmartfileConstructorOptions) {
if (optionsArg.contentBuffer) { if (optionsArg.contentBuffer) {
this.contentBuffer = optionsArg.contentBuffer this.contentBuffer = optionsArg.contentBuffer
this.contents = optionsArg.contentBuffer
} else if (optionsArg.contentString) { } else if (optionsArg.contentString) {
this.contentBuffer = optionsArg.contentBuffer this.setContentsFromString(optionsArg.contentString)
this.contents = Buffer.from(optionsArg.contentString)
} else { } else {
console.log('created empty Smartfile?') console.log('created empty Smartfile?')
} }
this.path = optionsArg.path this.path = optionsArg.path
this.cwd = optionsArg.cwd this.base = optionsArg.base
}
/**
* return relative path of file
* ->
*/
get relative () {
return ''
} }
@ -84,4 +69,59 @@ export class Smartfile {
*/ */
async read () { async read () {
} }
// -----------------------------------------------
// vinyl compatibility
// -----------------------------------------------
/**
* vinyl-compatibility: alias of this.contentBuffer
*/
get contents (): Buffer {
return this.contentBuffer
}
set contents (contentsArg) {
this.contentBuffer = contentsArg
}
/**
* vinyl-compatibility
*/
get cwd () {
return this.base
}
/**
* return relative path of file
*/
get relative (): string {
return this.path
}
/**
* return truw when the file has content
*/
isNull (): boolean {
if (!this.contentBuffer) {
return true
}
return false
}
/**
* return true if contents are Buffer
*/
isBuffer (): boolean {
if (this.contents instanceof Buffer) {
return true
}
return false
}
isDirectory () {
return false
}
isStream () {
return false
}
} }

View File

@ -221,10 +221,11 @@ export let fileTreeToObject = async (dirPathArg: string, miniMatchFilter: string
let smartfileArray: Smartfile[] = [] let smartfileArray: Smartfile[] = []
for (let filePath of fileTree) { for (let filePath of fileTree) {
smartfileArray.push(new Smartfile({ smartfileArray.push(new Smartfile({
path: filePath,
contentBuffer: new Buffer(toStringSync( contentBuffer: new Buffer(toStringSync(
plugins.path.join(dirPath, filePath) plugins.path.join(dirPath, filePath)
)) )),
base: dirPath,
path: filePath
})) }))
} }
return smartfileArray return smartfileArray