From 70a1f40bc8ef810b4272d37553cf86c6785be7f8 Mon Sep 17 00:00:00 2001 From: Dominique Rau Date: Fri, 8 Jan 2016 16:27:40 +0100 Subject: [PATCH] Update according to https://github.com/gulpjs/vinyl/releases/tag/v1.1.0 (fix) Add missing semicolons and tests mend --- vinyl/vinyl-0.4.3.d.ts | 108 +++++++ vinyl/vinyl-0.4.3.tests.ts | 560 +++++++++++++++++++++++++++++++++++++ vinyl/vinyl-tests.ts | 170 ++++++++--- vinyl/vinyl.d.ts | 42 ++- 4 files changed, 840 insertions(+), 40 deletions(-) create mode 100644 vinyl/vinyl-0.4.3.d.ts create mode 100644 vinyl/vinyl-0.4.3.tests.ts diff --git a/vinyl/vinyl-0.4.3.d.ts b/vinyl/vinyl-0.4.3.d.ts new file mode 100644 index 000000000..3669de261 --- /dev/null +++ b/vinyl/vinyl-0.4.3.d.ts @@ -0,0 +1,108 @@ +// Type definitions for vinyl 0.4.3 +// Project: https://github.com/wearefractal/vinyl +// Definitions by: vvakame , jedmao +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "vinyl" { + + import fs = require("fs"); + + /** + * A virtual file format. + */ + class File { + constructor(options?: { + /** + * Default: process.cwd() + */ + cwd?: string; + /** + * Used for relative pathing. Typically where a glob starts. + */ + base?: string; + /** + * Full path to the file. + */ + path?: string; + /** + * Path history. Has no effect if options.path is passed. + */ + history?: string[]; + /** + * The result of an fs.stat call. See fs.Stats for more information. + */ + stat?: fs.Stats; + /** + * File contents. + * Type: Buffer, Stream, or null + */ + contents?: Buffer | NodeJS.ReadWriteStream; + }); + + /** + * Default: process.cwd() + */ + public cwd: string; + /** + * Used for relative pathing. Typically where a glob starts. + */ + public base: string; + /** + * Full path to the file. + */ + public path: string; + public stat: fs.Stats; + /** + * Type: Buffer|Stream|null (Default: null) + */ + public contents: Buffer | NodeJS.ReadableStream; + /** + * Returns path.relative for the file base and file path. + * Example: + * var file = new File({ + * cwd: "/", + * base: "/test/", + * path: "/test/file.js" + * }); + * console.log(file.relative); // file.js + */ + public relative: string; + + public isBuffer(): boolean; + + public isStream(): boolean; + + public isNull(): boolean; + + public isDirectory(): boolean; + + /** + * Returns a new File object with all attributes cloned. Custom attributes are deep-cloned. + */ + public clone(opts?: { contents?: boolean }): File; + + /** + * If file.contents is a Buffer, it will write it to the stream. + * If file.contents is a Stream, it will pipe it to the stream. + * If file.contents is null, it will do nothing. + */ + public pipe( + stream: T, + opts?: { + /** + * If false, the destination stream will not be ended (same as node core). + */ + end?: boolean; + }): T; + + /** + * Returns a pretty String interpretation of the File. Useful for console.log. + */ + public inspect(): string; + } + + export = File; + +} \ No newline at end of file diff --git a/vinyl/vinyl-0.4.3.tests.ts b/vinyl/vinyl-0.4.3.tests.ts new file mode 100644 index 000000000..22a7d775a --- /dev/null +++ b/vinyl/vinyl-0.4.3.tests.ts @@ -0,0 +1,560 @@ +/// +/// + +/// + +import File = require('vinyl'); +import Stream = require('stream'); +import fs = require('fs'); + +declare var fakeStream: NodeJS.ReadWriteStream; + +describe('File', () => { + + describe('constructor()', () => { + + it('should default cwd to process.cwd', done => { + var file = new File(); + file.cwd.should.equal(process.cwd()); + done(); + }); + + it('should default base to cwd', done => { + var cwd = "/"; + var file = new File({cwd: cwd}); + file.base.should.equal(cwd); + done(); + }); + + it('should default base to cwd even when none is given', done => { + var file = new File(); + file.base.should.equal(process.cwd()); + done(); + }); + + it('should default path to null', done => { + var file = new File(); + should.not.exist(file.path); + done(); + }); + + it('should default stat to null', done => { + var file = new File(); + should.not.exist(file.stat); + done(); + }); + + it('should default contents to null', done => { + var file = new File(); + should.not.exist(file.contents); + done(); + }); + + it('should set base to given value', done => { + var val = "/"; + var file = new File({base: val}); + file.base.should.equal(val); + done(); + }); + + it('should set cwd to given value', done => { + var val = "/"; + var file = new File({cwd: val}); + file.cwd.should.equal(val); + done(); + }); + + it('should set path to given value', done => { + var val = "/test.coffee"; + var file = new File({path: val}); + file.path.should.equal(val); + done(); + }); + + it('should set stat to given value', done => { + var val = {}; + var file = new File({stat: val}); + file.stat.should.equal(val); + done(); + }); + + it('should set contents to given value', done => { + var val = new Buffer("test"); + var file = new File({contents: val}); + file.contents.should.equal(val); + done(); + }); + }); + + describe('isBuffer()', () => { + it('should return true when the contents are a Buffer', done => { + var val = new Buffer("test"); + var file = new File({contents: val}); + file.isBuffer().should.equal(true); + done(); + }); + + it('should return false when the contents are a Stream', done => { + var file = new File({ contents: fakeStream}); + file.isBuffer().should.equal(false); + done(); + }); + + it('should return false when the contents are a null', done => { + var file = new File({contents: null}); + file.isBuffer().should.equal(false); + done(); + }); + }); + + describe('isStream()', () => { + it('should return false when the contents are a Buffer', done => { + var val = new Buffer("test"); + var file = new File({contents: val}); + file.isStream().should.equal(false); + done(); + }); + + it('should return true when the contents are a Stream', done => { + var file = new File({ contents: fakeStream}); + file.isStream().should.equal(true); + done(); + }); + + it('should return false when the contents are a null', done => { + var file = new File({contents: null}); + file.isStream().should.equal(false); + done(); + }); + }); + + describe('isNull()', () => { + it('should return false when the contents are a Buffer', done => { + var val = new Buffer("test"); + var file = new File({contents: val}); + file.isNull().should.equal(false); + done(); + }); + + it('should return false when the contents are a Stream', done => { + var file = new File({ contents: fakeStream}); + file.isNull().should.equal(false); + done(); + }); + + it('should return true when the contents are a null', done => { + var file = new File({contents: null}); + file.isNull().should.equal(true); + done(); + }); + }); + + describe('isDirectory()', () => { + var fakeStat = { + isDirectory() { + return true; + } + }; + + it('should return false when the contents are a Buffer', done => { + var val = new Buffer("test"); + var file = new File({contents: val, stat: fakeStat}); + file.isDirectory().should.equal(false); + done(); + }); + + it('should return false when the contents are a Stream', done => { + var file = new File({ contents: fakeStream, stat: fakeStat}); + file.isDirectory().should.equal(false); + done(); + }); + + it('should return true when the contents are a null', done => { + var file = new File({contents: null, stat: fakeStat}); + file.isDirectory().should.equal(true); + done(); + }); + }); + + describe('clone()', () => { + it('should copy all attributes over with Buffer', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: new Buffer("test") + }; + var file = new File(options); + var file2 = file.clone(); + + file2.should.not.equal(file, 'refs should be different'); + file2.cwd.should.equal(file.cwd); + file2.base.should.equal(file.base); + file2.path.should.equal(file.path); + + let fileContents = file.contents; + let file2Contents = file2.contents; + + file2Contents.should.not.equal(fileContents, 'buffer ref should be different'); + + let fileUtf8Contents = fileContents instanceof Buffer ? + fileContents.toString('utf8') : + (fileContents).toString(); + let file2Utf8Contents = file2Contents instanceof Buffer ? + file2Contents.toString('utf8') : + (file2Contents).toString(); + + file2Utf8Contents.should.equal(fileUtf8Contents); + done(); + }); + + it('should copy all attributes over with Stream', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: fakeStream + }; + var file = new File(options); + var file2 = file.clone(); + + file2.should.not.equal(file, 'refs should be different'); + file2.cwd.should.equal(file.cwd); + file2.base.should.equal(file.base); + file2.path.should.equal(file.path); + file2.contents.should.equal(file.contents, 'stream ref should be the same'); + done(); + }); + + it('should copy all attributes over with null', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: fakeStream + }; + var file = new File(options); + var file2 = file.clone(); + + file2.should.not.equal(file, 'refs should be different'); + file2.cwd.should.equal(file.cwd); + file2.base.should.equal(file.base); + file2.path.should.equal(file.path); + should.not.exist(file2.contents); + done(); + }); + + it('should properly clone the `stat` property', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.js", + contents: new Buffer("test"), + stat: fs.statSync(__filename) + }; + + var file = new File(options); + var copy = file.clone(); + + // ReSharper disable WrongExpressionStatement + copy.stat.isFile().should.be.true; + copy.stat.isDirectory().should.be.false; + // ReSharper restore WrongExpressionStatement + + done(); + }); + }); + + describe('pipe()', () => { + it('should write to stream with Buffer', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: new Buffer("test") + }; + var file = new File(options); + var stream = new Stream.PassThrough(); + stream.on('data', (chunk: any) => { + should.exist(chunk); + (chunk instanceof Buffer).should.equal(true, 'should write as a buffer'); + chunk.toString('utf8').should.equal(options.contents.toString('utf8')); + }); + stream.on('end', () => { + done(); + }); + var ret = file.pipe(stream); + ret.should.equal(stream, 'should return the stream'); + }); + + it('should pipe to stream with Stream', done => { + var testChunk = new Buffer("test"); + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: new Stream.PassThrough() + }; + var file = new File(options); + var stream = new Stream.PassThrough(); + stream.on('data', (chunk: any) => { + should.exist(chunk); + (chunk instanceof Buffer).should.equal(true, 'should write as a buffer'); + chunk.toString('utf8').should.equal(testChunk.toString('utf8')); + done(); + }); + var ret = file.pipe(stream); + ret.should.equal(stream, 'should return the stream'); + + let fileContents = file.contents; + if (fileContents instanceof Buffer) { + fileContents.write(testChunk.toString()); + } + }); + + it('should do nothing with null', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: fakeStream + }; + var file = new File(options); + var stream = new Stream.PassThrough(); + stream.on('data', () => { + throw new Error("should not write"); + }); + stream.on('end', () => { + done(); + }); + var ret = file.pipe(stream); + ret.should.equal(stream, 'should return the stream'); + }); + + it('should write to stream with Buffer', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: new Buffer("test") + }; + var file = new File(options); + var stream = new Stream.PassThrough(); + stream.on('data', (chunk: any) => { + should.exist(chunk); + (chunk instanceof Buffer).should.equal(true, 'should write as a buffer'); + chunk.toString('utf8').should.equal(options.contents.toString('utf8')); + done(); + }); + stream.on('end', () => { + throw new Error("should not end"); + }); + var ret = file.pipe(stream, {end: false}); + ret.should.equal(stream, 'should return the stream'); + }); + + it('should pipe to stream with Stream', done => { + var testChunk = new Buffer("test"); + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: new Stream.PassThrough() + }; + var file = new File(options); + var stream = new Stream.PassThrough(); + stream.on('data', (chunk: any) => { + should.exist(chunk); + (chunk instanceof Buffer).should.equal(true, 'should write as a buffer'); + chunk.toString('utf8').should.equal(testChunk.toString('utf8')); + done(); + }); + stream.on('end', () => { + throw new Error("should not end"); + }); + var ret = file.pipe(stream, {end: false}); + ret.should.equal(stream, 'should return the stream'); + + let fileContents = file.contents; + if (fileContents instanceof Buffer) { + fileContents.write(testChunk.toString()); + } + }); + + it('should do nothing with null', done => { + var options = { + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: fakeStream + }; + var file = new File(options); + var stream = new Stream.PassThrough(); + stream.on('data', () => { + throw new Error("should not write"); + }); + stream.on('end', () => { + throw new Error("should not end"); + }); + var ret = file.pipe(stream, {end: false}); + ret.should.equal(stream, 'should return the stream'); + process.nextTick(done); + }); + }); + + describe('inspect()', () => { + it('should return correct format when no contents and no path', done => { + var file = new File(); + file.inspect().should.equal(''); + done(); + }); + + it('should return correct format when Buffer and no path', done => { + var val = new Buffer("test"); + var file = new File({ + contents: val + }); + file.inspect().should.equal('>'); + done(); + }); + + it('should return correct format when Buffer and relative path', done => { + var val = new Buffer("test"); + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: val + }); + file.inspect().should.equal('>'); + done(); + }); + + it('should return correct format when Buffer and only path and no base', done => { + var val = new Buffer("test"); + var file = new File({ + cwd: "/", + path: "/test/test.coffee", + contents: val + }); + delete file.base; + file.inspect().should.equal('>'); + done(); + }); + + it('should return correct format when Stream and relative path', done => { + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: new Stream.PassThrough() + }); + file.inspect().should.equal('>'); + done(); + }); + + it('should return correct format when null and relative path', done => { + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee", + contents: null + }); + file.inspect().should.equal(''); + done(); + }); + }); + + describe('contents get/set', () => { + it('should work with Buffer', done => { + var val = new Buffer("test"); + var file = new File(); + file.contents = val; + file.contents.should.equal(val); + done(); + }); + + it('should work with Stream', done => { + var val = new Stream.PassThrough(); + var file = new File(); + file.contents = val; + file.contents.should.equal(val); + done(); + }); + + it('should work with null', done => { + var file = new File(); + file.contents = null; + (file.contents === null).should.equal(true); + done(); + }); + + it('should not work with string', done => { + var val = "test"; + var file = new File(); + try { + file.contents = new Buffer(val); + } catch (err) { + should.exist(err); + done(); + } + }); + }); + + describe('relative get/set', () => { + it('should error on set', done => { + var file = new File(); + try { + file.relative = "test"; + } catch (err) { + should.exist(err); + done(); + } + }); + + it('should error on get when no base', done => { + var a: string; + var file = new File(); + delete file.base; + try { + // ReSharper disable once AssignedValueIsNeverUsed + a = file.relative; + } catch (err) { + should.exist(err); + done(); + } + }); + + it('should error on get when no path', done => { + var a: string; + var file = new File(); + try { + // ReSharper disable once AssignedValueIsNeverUsed + a = file.relative; + } catch (err) { + should.exist(err); + done(); + } + }); + + it('should return a relative path from base', done => { + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee" + }); + file.relative.should.equal("test.coffee"); + done(); + }); + + it('should return a relative path from cwd', done => { + var file = new File({ + cwd: "/", + path: "/test/test.coffee" + }); + file.relative.should.equal("test/test.coffee"); + done(); + }); + }); + +}); diff --git a/vinyl/vinyl-tests.ts b/vinyl/vinyl-tests.ts index cb1ceaea6..1d39646b1 100644 --- a/vinyl/vinyl-tests.ts +++ b/vinyl/vinyl-tests.ts @@ -22,13 +22,13 @@ describe('File', () => { it('should default base to cwd', done => { var cwd = "/"; var file = new File({cwd: cwd}); - file.base.should.equal(cwd); + file.basename.should.equal(cwd); done(); }); it('should default base to cwd even when none is given', done => { var file = new File(); - file.base.should.equal(process.cwd()); + file.basename.should.equal(process.cwd()); done(); }); @@ -53,7 +53,7 @@ describe('File', () => { it('should set base to given value', done => { var val = "/"; var file = new File({base: val}); - file.base.should.equal(val); + file.basename.should.equal(val); done(); }); @@ -84,6 +84,41 @@ describe('File', () => { file.contents.should.equal(val); done(); }); + + it('should default basename to cwd', done => { + var cwd = "/"; + var file = new File({cwd: cwd}); + file.basename.should.equal(cwd); + done(); + }); + + it('should default basename to cwd even when none is given', done => { + var file = new File(); + file.basename.should.equal(process.cwd()); + done(); + }); + + it('should set basename to given value', done => { + var val = "/"; + var file = new File({base: val}); + file.basename.should.equal(val); + done(); + }); + + it('should default extname to null', done => { + var cwd = "/"; + var file = new File({cwd: cwd}); + should.not.exist(file.path); + done(); + }); + + it('should default dirname to null', done => { + var cwd = "/"; + var file = new File({cwd: cwd}); + should.not.exist(file.dirname); + done(); + }); + }); describe('isBuffer()', () => { @@ -149,33 +184,6 @@ describe('File', () => { }); }); - describe('isDirectory()', () => { - var fakeStat = { - isDirectory() { - return true; - } - }; - - it('should return false when the contents are a Buffer', done => { - var val = new Buffer("test"); - var file = new File({contents: val, stat: fakeStat}); - file.isDirectory().should.equal(false); - done(); - }); - - it('should return false when the contents are a Stream', done => { - var file = new File({ contents: fakeStream, stat: fakeStat}); - file.isDirectory().should.equal(false); - done(); - }); - - it('should return true when the contents are a null', done => { - var file = new File({contents: null, stat: fakeStat}); - file.isDirectory().should.equal(true); - done(); - }); - }); - describe('clone()', () => { it('should copy all attributes over with Buffer', done => { var options = { @@ -189,7 +197,7 @@ describe('File', () => { file2.should.not.equal(file, 'refs should be different'); file2.cwd.should.equal(file.cwd); - file2.base.should.equal(file.base); + file2.basename.should.equal(file.basename); file2.path.should.equal(file.path); let fileContents = file.contents; @@ -220,7 +228,7 @@ describe('File', () => { file2.should.not.equal(file, 'refs should be different'); file2.cwd.should.equal(file.cwd); - file2.base.should.equal(file.base); + file2.basename.should.equal(file.basename); file2.path.should.equal(file.path); file2.contents.should.equal(file.contents, 'stream ref should be the same'); done(); @@ -238,7 +246,7 @@ describe('File', () => { file2.should.not.equal(file, 'refs should be different'); file2.cwd.should.equal(file.cwd); - file2.base.should.equal(file.base); + file2.basename.should.equal(file.basename); file2.path.should.equal(file.path); should.not.exist(file2.contents); done(); @@ -258,7 +266,6 @@ describe('File', () => { // ReSharper disable WrongExpressionStatement copy.stat.isFile().should.be.true; - copy.stat.isDirectory().should.be.false; // ReSharper restore WrongExpressionStatement done(); @@ -437,7 +444,7 @@ describe('File', () => { path: "/test/test.coffee", contents: val }); - delete file.base; + delete file.basename; file.inspect().should.equal('>'); done(); }); @@ -515,7 +522,7 @@ describe('File', () => { it('should error on get when no base', done => { var a: string; var file = new File(); - delete file.base; + delete file.basename; try { // ReSharper disable once AssignedValueIsNeverUsed a = file.relative; @@ -557,4 +564,95 @@ describe('File', () => { }); }); + describe('path get/set', () => { + + it('should return an absolute path', done => { + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee" + }); + file.path.should.equal("/test/test.coffee"); + done(); + }); + + }); + + describe('history get', () => { + it('should error on set', done => { + var file = new File(); + try { + file.history = []; + } catch (err) { + should.exist(err); + done(); + } + }); + + it('should return an history', done => { + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee" + }); + file.history.should.equal(["/test/test.coffee"]); + done(); + }); + + }); + + describe('dirname get', () => { + + it('should return an dirname', done => { + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee" + }); + file.dirname.should.equal("test"); + done(); + }); + + it('should set dirname to given value', done => { + var file = new File(); + file.dirname = ".ext" + file.dirname.should.equal(".ext") + done(); + }); + + it('should set dirname to null', done => { + var file = new File(); + file.dirname = null + should.not.exist(file.dirname) + done(); + }); + }); + + describe('extname get/set', () => { + + it('should return an extname', done => { + var file = new File({ + cwd: "/", + base: "/test/", + path: "/test/test.coffee" + }); + file.dirname.should.equal(".coffee"); + done(); + }); + + it('should set extname to given value', done => { + var file = new File(); + file.extname = ".ext" + file.extname.should.equal(".ext") + done(); + }); + + it('should set extname to null', done => { + var file = new File(); + file.extname = null + should.not.exist(file.extname) + done(); + }); + }); + }); diff --git a/vinyl/vinyl.d.ts b/vinyl/vinyl.d.ts index 39960361f..77bf252b7 100644 --- a/vinyl/vinyl.d.ts +++ b/vinyl/vinyl.d.ts @@ -1,4 +1,4 @@ -// Type definitions for vinyl 0.4.3 +// Type definitions for vinyl 1.1.0 // Project: https://github.com/wearefractal/vinyl // Definitions by: vvakame , jedmao // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -14,26 +14,32 @@ declare module "vinyl" { */ class File { constructor(options?: { + /** * Default: process.cwd() */ cwd?: string; + /** * Used for relative pathing. Typically where a glob starts. */ base?: string; + /** * Full path to the file. */ path?: string; + /** * Path history. Has no effect if options.path is passed. */ history?: string[]; + /** * The result of an fs.stat call. See fs.Stats for more information. */ stat?: fs.Stats; + /** * File contents. * Type: Buffer, Stream, or null @@ -45,19 +51,40 @@ declare module "vinyl" { * Default: process.cwd() */ public cwd: string; + /** * Used for relative pathing. Typically where a glob starts. */ + public dirname: string; + public basename: string; public base: string; + /** * Full path to the file. */ public path: string; public stat: fs.Stats; + + /** + * Gets and sets stem (filename without suffix) for the file path. + */ + public stem: string; + + /** + * Gets and sets path.extname for the file path + */ + public extname: string; + + /** + * Array of path values the file object has had + */ + public history: string[]; + /** * Type: Buffer|Stream|null (Default: null) */ public contents: Buffer | NodeJS.ReadableStream; + /** * Returns path.relative for the file base and file path. * Example: @@ -70,18 +97,25 @@ declare module "vinyl" { */ public relative: string; + /** + * Returns true if file.contents is a Buffer. + */ public isBuffer(): boolean; + /** + * Returns true if file.contents is a Stream. + */ public isStream(): boolean; + /** + * Returns true if file.contents is null. + */ public isNull(): boolean; - public isDirectory(): boolean; - /** * Returns a new File object with all attributes cloned. Custom attributes are deep-cloned. */ - public clone(opts?: { contents?: boolean }): File; + public clone(opts?: { contents?: boolean, deep?:boolean }): File; /** * If file.contents is a Buffer, it will write it to the stream.