diff --git a/Gruntfile.coffee b/Gruntfile.coffee deleted file mode 100644 index 6ecc3d0..0000000 --- a/Gruntfile.coffee +++ /dev/null @@ -1,36 +0,0 @@ -module.exports = (grunt) -> - grunt.initConfig - pkg: grunt.file.readJSON('package.json') - - coffee: - glob_to_multiple: - expand: true - cwd: 'src' - src: ['*.coffee'] - dest: 'lib' - ext: '.js' - - shell: - test: - command: 'jasmine-focused --coffee --captureExceptions spec' - options: - stdout: true - stderr: true - failOnError: true - - coffeelint: - options: - no_empty_param_list: - level: 'error' - max_line_length: - level: 'ignore' - src: ['src/**/*.coffee'] - test: ['spec/*.coffee'] - - grunt.loadNpmTasks('grunt-coffeelint') - grunt.loadNpmTasks('grunt-contrib-coffee') - grunt.loadNpmTasks('grunt-shell') - grunt.registerTask 'clean', -> require('rimraf').sync('lib') - grunt.registerTask('lint', ['coffeelint']) - grunt.registerTask('default', ['clean', 'lint', 'coffee']) - grunt.registerTask('test', ['default', 'shell:test']) diff --git a/package.json b/package.json index 1bc189b..9c775c7 100644 --- a/package.json +++ b/package.json @@ -30,17 +30,10 @@ "lsa": "./bin/lsa" }, "devDependencies": { - "grunt": "~0.4.0", - "grunt-contrib-coffee": "~0.9.0", - "coffeescript": "~1.7.0", - "grunt-cli": "~0.1.6", - "grunt-shell": "~0.2.2", - "jasmine-focused": "1.x", - "grunt-coffeelint": "0.0.6" + "jasmine-focused": "1.x" }, "scripts": { - "prepublish": "grunt", - "test": "grunt test" + "test": "jasmine-focused --captureExceptions spec" }, "dependencies": { "tar": "^2.2.1", diff --git a/spec/bzip-spec.coffee b/spec/bzip-spec.coffee deleted file mode 100644 index 2012414..0000000 --- a/spec/bzip-spec.coffee +++ /dev/null @@ -1,198 +0,0 @@ -archive = require '../src/ls-archive' -path = require 'path' - -describe "bzipped tar files", -> - fixturesRoot = null - - beforeEach -> - fixturesRoot = path.join(__dirname, 'fixtures') - - describe ".list()", -> - describe "when the archive file exists", -> - it "returns files in the bzipped tar archive", -> - bzipPaths = null - callback = (error, paths) -> bzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-file.tar.bz2'), callback) - waitsFor -> bzipPaths? - runs -> - expect(bzipPaths.length).toBe 1 - expect(bzipPaths[0].path).toBe 'file.txt' - expect(bzipPaths[0].isDirectory()).toBe false - expect(bzipPaths[0].isFile()).toBe true - expect(bzipPaths[0].isSymbolicLink()).toBe false - - it "returns files in the bzipped tar archive", -> - bzipPaths = null - callback = (error, paths) -> bzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-file.tbz'), callback) - waitsFor -> bzipPaths? - runs -> - expect(bzipPaths.length).toBe 1 - expect(bzipPaths[0].path).toBe 'file.txt' - expect(bzipPaths[0].isDirectory()).toBe false - expect(bzipPaths[0].isFile()).toBe true - expect(bzipPaths[0].isSymbolicLink()).toBe false - - it "returns files in the bzipped tar archive", -> - bzipPaths = null - callback = (error, paths) -> bzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-file.tbz2'), callback) - waitsFor -> bzipPaths? - runs -> - expect(bzipPaths.length).toBe 1 - expect(bzipPaths[0].path).toBe 'file.txt' - expect(bzipPaths[0].isDirectory()).toBe false - expect(bzipPaths[0].isFile()).toBe true - expect(bzipPaths[0].isSymbolicLink()).toBe false - - it "returns folders in the bzipped tar archive", -> - bzipPaths = null - callback = (error, paths) -> bzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-folder.tar.bz2'), callback) - waitsFor -> bzipPaths? - runs -> - expect(bzipPaths.length).toBe 1 - expect(bzipPaths[0].path).toBe 'folder' - expect(bzipPaths[0].isDirectory()).toBe true - expect(bzipPaths[0].isFile()).toBe false - expect(bzipPaths[0].isSymbolicLink()).toBe false - - it "returns folders in the bzipped tar archive", -> - bzipPaths = null - callback = (error, paths) -> bzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-folder.tbz'), callback) - waitsFor -> bzipPaths? - runs -> - expect(bzipPaths.length).toBe 1 - expect(bzipPaths[0].path).toBe 'folder' - expect(bzipPaths[0].isDirectory()).toBe true - expect(bzipPaths[0].isFile()).toBe false - expect(bzipPaths[0].isSymbolicLink()).toBe false - - it "returns folders in the bzipped tar archive", -> - bzipPaths = null - callback = (error, paths) -> bzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-folder.tbz2'), callback) - waitsFor -> bzipPaths? - runs -> - expect(bzipPaths.length).toBe 1 - expect(bzipPaths[0].path).toBe 'folder' - expect(bzipPaths[0].isDirectory()).toBe true - expect(bzipPaths[0].isFile()).toBe false - expect(bzipPaths[0].isSymbolicLink()).toBe false - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid bzipped tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar.bz2') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the second to last extension isn't .tar", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.txt.bz2') - pathError = null - callback = (error, contents) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe ".readFile()", -> - describe "when the path exists in the archive", -> - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'one-file.tar.bz2') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, 'file.txt', callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe 'hello\n' - - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'one-file.tbz') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, 'file.txt', callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe 'hello\n' - - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'one-file.tbz2') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, 'file.txt', callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe 'hello\n' - - describe "when the path does not exist in the archive", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'one-file.tar.bz2') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid bzipped tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar.bz2') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'invalid.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the second to last extension isn't .tar", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.txt.bz2') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'invalid.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe ".readBzip()", -> - it "calls back with the string contents of the archive", -> - archivePath = path.join(fixturesRoot, 'file.txt.bz2') - archiveContents = null - callback = (error, contents) -> archiveContents = contents - archive.readBzip(archivePath, callback) - waitsFor -> archiveContents? - runs -> expect(archiveContents.toString()).toBe 'hello\n' - - describe "when the archive path isn't a valid bzipped tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar.bz2') - readError = null - callback = (error, contents) -> readError = error - archive.readBzip(archivePath, callback) - waitsFor -> readError? - runs -> expect(readError.message.length).toBeGreaterThan 0 - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2') - readError = null - callback = (error, contents) -> readError = error - archive.readBzip(archivePath, callback) - waitsFor -> readError? - runs -> expect(readError.message.length).toBeGreaterThan 0 diff --git a/spec/bzip-spec.js b/spec/bzip-spec.js new file mode 100644 index 0000000..c477e9f --- /dev/null +++ b/spec/bzip-spec.js @@ -0,0 +1,219 @@ +const archive = require('../src/ls-archive'); +const path = require('path'); + +describe("bzipped tar files", function() { + let fixturesRoot = null; + + beforeEach(() => fixturesRoot = path.join(__dirname, 'fixtures')); + + describe(".list()", function() { + describe("when the archive file exists", function() { + it("returns files in the bzipped tar archive", function() { + let bzipPaths = null; + const callback = (error, paths) => bzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-file.tar.bz2'), callback); + waitsFor(() => bzipPaths != null); + runs(function() { + expect(bzipPaths.length).toBe(1); + expect(bzipPaths[0].path).toBe('file.txt'); + expect(bzipPaths[0].isDirectory()).toBe(false); + expect(bzipPaths[0].isFile()).toBe(true); + expect(bzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns files in the bzipped tar archive", function() { + let bzipPaths = null; + const callback = (error, paths) => bzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-file.tbz'), callback); + waitsFor(() => bzipPaths != null); + runs(function() { + expect(bzipPaths.length).toBe(1); + expect(bzipPaths[0].path).toBe('file.txt'); + expect(bzipPaths[0].isDirectory()).toBe(false); + expect(bzipPaths[0].isFile()).toBe(true); + expect(bzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns files in the bzipped tar archive", function() { + let bzipPaths = null; + const callback = (error, paths) => bzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-file.tbz2'), callback); + waitsFor(() => bzipPaths != null); + runs(function() { + expect(bzipPaths.length).toBe(1); + expect(bzipPaths[0].path).toBe('file.txt'); + expect(bzipPaths[0].isDirectory()).toBe(false); + expect(bzipPaths[0].isFile()).toBe(true); + expect(bzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns folders in the bzipped tar archive", function() { + let bzipPaths = null; + const callback = (error, paths) => bzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-folder.tar.bz2'), callback); + waitsFor(() => bzipPaths != null); + runs(function() { + expect(bzipPaths.length).toBe(1); + expect(bzipPaths[0].path).toBe('folder'); + expect(bzipPaths[0].isDirectory()).toBe(true); + expect(bzipPaths[0].isFile()).toBe(false); + expect(bzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns folders in the bzipped tar archive", function() { + let bzipPaths = null; + const callback = (error, paths) => bzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-folder.tbz'), callback); + waitsFor(() => bzipPaths != null); + runs(function() { + expect(bzipPaths.length).toBe(1); + expect(bzipPaths[0].path).toBe('folder'); + expect(bzipPaths[0].isDirectory()).toBe(true); + expect(bzipPaths[0].isFile()).toBe(false); + expect(bzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns folders in the bzipped tar archive", function() { + let bzipPaths = null; + const callback = (error, paths) => bzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-folder.tbz2'), callback); + waitsFor(() => bzipPaths != null); + runs(function() { + expect(bzipPaths.length).toBe(1); + expect(bzipPaths[0].path).toBe('folder'); + expect(bzipPaths[0].isDirectory()).toBe(true); + expect(bzipPaths[0].isFile()).toBe(false); + expect(bzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + }); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid bzipped tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar.bz2'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the second to last extension isn't .tar", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.txt.bz2'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + }); + + describe(".readFile()", function() { + describe("when the path exists in the archive", function() { + it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tar.bz2'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, 'file.txt', callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('hello\n')); + }); + + it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tbz'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, 'file.txt', callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('hello\n')); + }); + + it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tbz2'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, 'file.txt', callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('hello\n')); + }); + }); + + describe("when the path does not exist in the archive", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tar.bz2'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid bzipped tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar.bz2'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'invalid.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the second to last extension isn't .tar", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.txt.bz2'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'invalid.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + }); + + describe(".readBzip()", function() { + it("calls back with the string contents of the archive", function() { + const archivePath = path.join(fixturesRoot, 'file.txt.bz2'); + let archiveContents = null; + const callback = (error, contents) => archiveContents = contents; + archive.readBzip(archivePath, callback); + waitsFor(() => archiveContents != null); + runs(() => expect(archiveContents.toString()).toBe('hello\n')); + }); + + describe("when the archive path isn't a valid bzipped tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar.bz2'); + let readError = null; + const callback = (error, contents) => readError = error; + archive.readBzip(archivePath, callback); + waitsFor(() => readError != null); + runs(() => expect(readError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar.bz2'); + let readError = null; + const callback = (error, contents) => readError = error; + archive.readBzip(archivePath, callback); + waitsFor(() => readError != null); + runs(() => expect(readError.message.length).toBeGreaterThan(0)); + })); + }); +}); diff --git a/spec/common-spec.coffee b/spec/common-spec.coffee deleted file mode 100644 index ae49c2f..0000000 --- a/spec/common-spec.coffee +++ /dev/null @@ -1,45 +0,0 @@ -archive = require '../src/ls-archive' -path = require 'path' - -describe "Common behavior", -> - describe ".list()", -> - it "calls back with an error for unsupported extensions", -> - pathError = null - callback = (error) -> pathError = error - archive.list(path.join('tmp', 'file.txt'), callback) - waitsFor -> pathError? - runs -> expect(pathError.message).not.toBeNull() - - it "returns undefined", -> - expect(archive.list(path.join('tmp', 'file.zip'), ->)).toBeUndefined() - - describe ".readFile()", -> - it "calls back with an error for unsupported extensions", -> - pathError = null - callback = (error) -> pathError = error - archive.readFile(path.join('tmp', 'file.txt'), 'file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message).not.toBeNull() - - it "returns undefined", -> - expect(archive.readFile(path.join('tmp', 'file.txt'), 'file.txt', ->)).toBeUndefined() - - describe ".isPathSupported()", -> - it "returns true for supported path extensions", -> - expect(archive.isPathSupported("#{path.sep}a.epub")).toBe true - expect(archive.isPathSupported("#{path.sep}a.zip")).toBe true - expect(archive.isPathSupported("#{path.sep}a.jar")).toBe true - expect(archive.isPathSupported("#{path.sep}a.war")).toBe true - expect(archive.isPathSupported("#{path.sep}a.tar")).toBe true - expect(archive.isPathSupported("#{path.sep}a.tgz")).toBe true - expect(archive.isPathSupported("#{path.sep}a.tar.gz")).toBe true - expect(archive.isPathSupported("#{path.sep}a.whl")).toBe true - expect(archive.isPathSupported("#{path.sep}a.egg")).toBe true - expect(archive.isPathSupported("#{path.sep}a.xpi")).toBe true - expect(archive.isPathSupported("#{path.sep}a.nupkg")).toBe true - expect(archive.isPathSupported("#{path.sep}a.bar.gz")).toBe false - expect(archive.isPathSupported("#{path.sep}a.txt")).toBe false - expect(archive.isPathSupported("#{path.sep}")).toBe false - expect(archive.isPathSupported('')).toBe false - expect(archive.isPathSupported(null)).toBe false - expect(archive.isPathSupported()).toBe false diff --git a/spec/common-spec.js b/spec/common-spec.js new file mode 100644 index 0000000..978c75f --- /dev/null +++ b/spec/common-spec.js @@ -0,0 +1,48 @@ +const archive = require('../src/ls-archive'); +const path = require('path'); + +describe("Common behavior", function() { + describe(".list()", function() { + it("calls back with an error for unsupported extensions", function() { + let pathError = null; + const callback = error => pathError = error; + archive.list(path.join('tmp', 'file.txt'), callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message).not.toBeNull()); + }); + + it("returns undefined", () => expect(archive.list(path.join('tmp', 'file.zip'), function() {})).toBeUndefined()); + }); + + describe(".readFile()", function() { + it("calls back with an error for unsupported extensions", function() { + let pathError = null; + const callback = error => pathError = error; + archive.readFile(path.join('tmp', 'file.txt'), 'file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message).not.toBeNull()); + }); + + it("returns undefined", () => expect(archive.readFile(path.join('tmp', 'file.txt'), 'file.txt', function() {})).toBeUndefined()); + }); + + describe(".isPathSupported()", () => it("returns true for supported path extensions", function() { + expect(archive.isPathSupported(`${path.sep}a.epub`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.zip`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.jar`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.war`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.tar`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.tgz`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.tar.gz`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.whl`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.egg`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.xpi`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.nupkg`)).toBe(true); + expect(archive.isPathSupported(`${path.sep}a.bar.gz`)).toBe(false); + expect(archive.isPathSupported(`${path.sep}a.txt`)).toBe(false); + expect(archive.isPathSupported(`${path.sep}`)).toBe(false); + expect(archive.isPathSupported('')).toBe(false); + expect(archive.isPathSupported(null)).toBe(false); + expect(archive.isPathSupported()).toBe(false); + })); +}); diff --git a/spec/gzip-spec.coffee b/spec/gzip-spec.coffee deleted file mode 100644 index 5d4bcb7..0000000 --- a/spec/gzip-spec.coffee +++ /dev/null @@ -1,166 +0,0 @@ -archive = require '../src/ls-archive' -path = require 'path' - -describe "gzipped tar files", -> - fixturesRoot = null - - beforeEach -> - fixturesRoot = path.join(__dirname, 'fixtures') - - describe ".list()", -> - describe "when the archive file exists", -> - it "returns files in the gzipped tar archive", -> - gzipPaths = null - callback = (error, paths) -> gzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-file.tar.gz'), callback) - waitsFor -> gzipPaths? - runs -> - expect(gzipPaths.length).toBe 1 - expect(gzipPaths[0].path).toBe 'file.txt' - expect(gzipPaths[0].isDirectory()).toBe false - expect(gzipPaths[0].isFile()).toBe true - expect(gzipPaths[0].isSymbolicLink()).toBe false - - it "returns files in the gzipped tar archive", -> - gzipPaths = null - callback = (error, paths) -> gzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-file.tgz'), callback) - waitsFor -> gzipPaths? - runs -> - expect(gzipPaths.length).toBe 1 - expect(gzipPaths[0].path).toBe 'file.txt' - expect(gzipPaths[0].isDirectory()).toBe false - expect(gzipPaths[0].isFile()).toBe true - expect(gzipPaths[0].isSymbolicLink()).toBe false - - it "returns folders in the gzipped tar archive", -> - gzipPaths = null - callback = (error, paths) -> gzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-folder.tar.gz'), callback) - waitsFor -> gzipPaths? - runs -> - expect(gzipPaths.length).toBe 1 - expect(gzipPaths[0].path).toBe 'folder' - expect(gzipPaths[0].isDirectory()).toBe true - expect(gzipPaths[0].isFile()).toBe false - expect(gzipPaths[0].isSymbolicLink()).toBe false - - it "returns folders in the gzipped tar archive", -> - gzipPaths = null - callback = (error, paths) -> gzipPaths = paths - archive.list(path.join(fixturesRoot, 'one-folder.tgz'), callback) - waitsFor -> gzipPaths? - runs -> - expect(gzipPaths.length).toBe 1 - expect(gzipPaths[0].path).toBe 'folder' - expect(gzipPaths[0].isDirectory()).toBe true - expect(gzipPaths[0].isFile()).toBe false - expect(gzipPaths[0].isSymbolicLink()).toBe false - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar.gz') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid gzipped tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar.gz') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the second to last extension isn't .tar", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.txt.gz') - pathError = null - callback = (error, contents) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe ".readFile()", -> - describe "when the path exists in the archive", -> - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'one-file.tar.gz') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, 'file.txt', callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe 'hello\n' - - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'one-file.tgz') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, 'file.txt', callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe 'hello\n' - - describe "when the path does not exist in the archive", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'one-file.tar.gz') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar.gz') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid gzipped tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar.gz') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'invalid.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the second to last extension isn't .tar", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.txt.gz') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'invalid.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe ".readGzip()", -> - it "calls back with the string contents of the archive", -> - archivePath = path.join(fixturesRoot, 'file.txt.gz') - archiveContents = null - callback = (error, contents) -> archiveContents = contents - archive.readGzip(archivePath, callback) - waitsFor -> archiveContents? - runs -> expect(archiveContents.toString()).toBe 'hello\n' - - describe "when the archive path isn't a valid gzipped tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar.gz') - readError = null - callback = (error, contents) -> readError = error - archive.readGzip(archivePath, callback) - waitsFor -> readError? - runs -> expect(readError.message.length).toBeGreaterThan 0 - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar.gz') - readError = null - callback = (error, contents) -> readError = error - archive.readGzip(archivePath, callback) - waitsFor -> readError? - runs -> expect(readError.message.length).toBeGreaterThan 0 diff --git a/spec/gzip-spec.js b/spec/gzip-spec.js new file mode 100644 index 0000000..0fbf0b4 --- /dev/null +++ b/spec/gzip-spec.js @@ -0,0 +1,182 @@ +const archive = require('../src/ls-archive'); +const path = require('path'); + +describe("gzipped tar files", function() { + let fixturesRoot = null; + + beforeEach(() => fixturesRoot = path.join(__dirname, 'fixtures')); + + describe(".list()", function() { + describe("when the archive file exists", function() { + it("returns files in the gzipped tar archive", function() { + let gzipPaths = null; + const callback = (error, paths) => gzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-file.tar.gz'), callback); + waitsFor(() => gzipPaths != null); + runs(function() { + expect(gzipPaths.length).toBe(1); + expect(gzipPaths[0].path).toBe('file.txt'); + expect(gzipPaths[0].isDirectory()).toBe(false); + expect(gzipPaths[0].isFile()).toBe(true); + expect(gzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns files in the gzipped tar archive", function() { + let gzipPaths = null; + const callback = (error, paths) => gzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-file.tgz'), callback); + waitsFor(() => gzipPaths != null); + runs(function() { + expect(gzipPaths.length).toBe(1); + expect(gzipPaths[0].path).toBe('file.txt'); + expect(gzipPaths[0].isDirectory()).toBe(false); + expect(gzipPaths[0].isFile()).toBe(true); + expect(gzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns folders in the gzipped tar archive", function() { + let gzipPaths = null; + const callback = (error, paths) => gzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-folder.tar.gz'), callback); + waitsFor(() => gzipPaths != null); + runs(function() { + expect(gzipPaths.length).toBe(1); + expect(gzipPaths[0].path).toBe('folder'); + expect(gzipPaths[0].isDirectory()).toBe(true); + expect(gzipPaths[0].isFile()).toBe(false); + expect(gzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns folders in the gzipped tar archive", function() { + let gzipPaths = null; + const callback = (error, paths) => gzipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-folder.tgz'), callback); + waitsFor(() => gzipPaths != null); + runs(function() { + expect(gzipPaths.length).toBe(1); + expect(gzipPaths[0].path).toBe('folder'); + expect(gzipPaths[0].isDirectory()).toBe(true); + expect(gzipPaths[0].isFile()).toBe(false); + expect(gzipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + }); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar.gz'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid gzipped tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar.gz'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the second to last extension isn't .tar", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.txt.gz'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + }); + + describe(".readFile()", function() { + describe("when the path exists in the archive", function() { + it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tar.gz'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, 'file.txt', callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('hello\n')); + }); + + it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tgz'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, 'file.txt', callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('hello\n')); + }); + }); + + describe("when the path does not exist in the archive", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tar.gz'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar.gz'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid gzipped tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar.gz'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'invalid.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the second to last extension isn't .tar", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.txt.gz'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'invalid.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + }); + + describe(".readGzip()", function() { + it("calls back with the string contents of the archive", function() { + const archivePath = path.join(fixturesRoot, 'file.txt.gz'); + let archiveContents = null; + const callback = (error, contents) => archiveContents = contents; + archive.readGzip(archivePath, callback); + waitsFor(() => archiveContents != null); + runs(() => expect(archiveContents.toString()).toBe('hello\n')); + }); + + describe("when the archive path isn't a valid gzipped tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar.gz'); + let readError = null; + const callback = (error, contents) => readError = error; + archive.readGzip(archivePath, callback); + waitsFor(() => readError != null); + runs(() => expect(readError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar.gz'); + let readError = null; + const callback = (error, contents) => readError = error; + archive.readGzip(archivePath, callback); + waitsFor(() => readError != null); + runs(() => expect(readError.message.length).toBeGreaterThan(0)); + })); + }); +}); diff --git a/spec/tar-spec.coffee b/spec/tar-spec.coffee deleted file mode 100644 index 50ffe4b..0000000 --- a/spec/tar-spec.coffee +++ /dev/null @@ -1,147 +0,0 @@ -archive = require '../src/ls-archive' -path = require 'path' - -describe "tar files", -> - fixturesRoot = null - - beforeEach -> - fixturesRoot = path.join(__dirname, 'fixtures') - - describe ".list()", -> - describe "when the archive file exists", -> - it "returns files in the tar archive", -> - tarPaths = null - callback = (error, paths) -> tarPaths = paths - archive.list(path.join(fixturesRoot, 'one-file.tar'), callback) - waitsFor -> tarPaths? - runs -> - expect(tarPaths.length).toBe 1 - expect(tarPaths[0].path).toBe 'file.txt' - expect(tarPaths[0].isDirectory()).toBe false - expect(tarPaths[0].isFile()).toBe true - expect(tarPaths[0].isSymbolicLink()).toBe false - - it "returns folders in the tar archive", -> - tarPaths = null - callback = (error, paths) -> tarPaths = paths - archive.list(path.join(fixturesRoot, 'one-folder.tar'), callback) - waitsFor -> tarPaths? - runs -> - expect(tarPaths.length).toBe 1 - expect(tarPaths[0].path).toBe 'folder' - expect(tarPaths[0].isDirectory()).toBe true - expect(tarPaths[0].isFile()).toBe false - expect(tarPaths[0].isSymbolicLink()).toBe false - - describe "when the tree option is set to true", -> - describe "when the archive has no directories entries", -> - it "returns archive entries nested under their parent directory", -> - tree = null - archive.list path.join(__dirname, 'fixtures', 'no-dir-entries.tgz'), tree: true, (error, files) -> - tree = files - waitsFor -> tree? - runs -> - expect(tree.length).toBe 1 - expect(tree[0].getName()).toBe 'package' - expect(tree[0].getPath()).toBe 'package' - expect(tree[0].children.length).toBe 5 - expect(tree[0].children[0].getName()).toBe 'package.json' - expect(tree[0].children[0].getPath()).toBe path.join('package', 'package.json') - expect(tree[0].children[1].getName()).toBe 'README.md' - expect(tree[0].children[1].getPath()).toBe path.join('package', 'README.md') - expect(tree[0].children[2].getName()).toBe 'LICENSE.md' - expect(tree[0].children[2].getPath()).toBe path.join('package', 'LICENSE.md') - expect(tree[0].children[3].getName()).toBe 'bin' - expect(tree[0].children[3].getPath()).toBe path.join('package', 'bin') - expect(tree[0].children[4].children[0].getName()).toBe 'lister.js' - expect(tree[0].children[4].children[0].getPath()).toBe path.join('package', 'lib', 'lister.js') - expect(tree[0].children[4].children[1].getName()).toBe 'ls-archive-cli.js' - expect(tree[0].children[4].children[1].getPath()).toBe path.join('package', 'lib', 'ls-archive-cli.js') - expect(tree[0].children[4].children[2].getName()).toBe 'ls-archive.js' - expect(tree[0].children[4].children[2].getPath()).toBe path.join('package', 'lib', 'ls-archive.js') - expect(tree[0].children[4].children[3].getName()).toBe 'reader.js' - expect(tree[0].children[4].children[3].getPath()).toBe path.join('package', 'lib', 'reader.js') - - describe "when the archive has multiple directories at the root", -> - it "returns archive entries nested under their parent directory", -> - tree = null - archive.list path.join(__dirname, 'fixtures', 'nested.tar'), tree: true, (error, files) -> - tree = files - waitsFor -> tree? - runs -> - expect(tree.length).toBe 2 - - expect(tree[0].getPath()).toBe 'd1' - expect(tree[0].children[0].getName()).toBe 'd2' - expect(tree[0].children[0].children[0].getName()).toBe 'd3' - expect(tree[0].children[0].children[1].getName()).toBe 'f1.txt' - expect(tree[0].children[1].getName()).toBe 'd4' - expect(tree[0].children[2].getName()).toBe 'f2.txt' - - expect(tree[1].getPath()).toBe 'da' - expect(tree[1].children[0].getName()).toBe 'db' - expect(tree[1].children[1].getName()).toBe 'fa.txt' - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe ".readFile()", -> - describe "when the path exists in the archive", -> - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'one-file.tar') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, 'file.txt', callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe 'hello\n' - - describe "when the path does not exist in the archive", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'one-file.tar') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.tar') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid tar file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.tar') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'invalid.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the path is a folder", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'one-folder.tar') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, "folder#{path.sep}", callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 diff --git a/spec/tar-spec.js b/spec/tar-spec.js new file mode 100644 index 0000000..8c3e07f --- /dev/null +++ b/spec/tar-spec.js @@ -0,0 +1,155 @@ +const archive = require('../src/ls-archive'); +const path = require('path'); + +describe("tar files", function() { + let fixturesRoot = null; + + beforeEach(() => fixturesRoot = path.join(__dirname, 'fixtures')); + + describe(".list()", function() { + describe("when the archive file exists", function() { + it("returns files in the tar archive", function() { + let tarPaths = null; + const callback = (error, paths) => tarPaths = paths; + archive.list(path.join(fixturesRoot, 'one-file.tar'), callback); + waitsFor(() => tarPaths != null); + runs(function() { + expect(tarPaths.length).toBe(1); + expect(tarPaths[0].path).toBe('file.txt'); + expect(tarPaths[0].isDirectory()).toBe(false); + expect(tarPaths[0].isFile()).toBe(true); + expect(tarPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns folders in the tar archive", function() { + let tarPaths = null; + const callback = (error, paths) => tarPaths = paths; + archive.list(path.join(fixturesRoot, 'one-folder.tar'), callback); + waitsFor(() => tarPaths != null); + runs(function() { + expect(tarPaths.length).toBe(1); + expect(tarPaths[0].path).toBe('folder'); + expect(tarPaths[0].isDirectory()).toBe(true); + expect(tarPaths[0].isFile()).toBe(false); + expect(tarPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + describe("when the tree option is set to true", function() { + describe("when the archive has no directories entries", () => it("returns archive entries nested under their parent directory", function() { + let tree = null; + archive.list(path.join(__dirname, 'fixtures', 'no-dir-entries.tgz'), {tree: true}, (error, files) => tree = files); + waitsFor(() => tree != null); + runs(function() { + expect(tree.length).toBe(1); + expect(tree[0].getName()).toBe('package'); + expect(tree[0].getPath()).toBe('package'); + expect(tree[0].children.length).toBe(5); + expect(tree[0].children[0].getName()).toBe('package.json'); + expect(tree[0].children[0].getPath()).toBe(path.join('package', 'package.json')); + expect(tree[0].children[1].getName()).toBe('README.md'); + expect(tree[0].children[1].getPath()).toBe(path.join('package', 'README.md')); + expect(tree[0].children[2].getName()).toBe('LICENSE.md'); + expect(tree[0].children[2].getPath()).toBe(path.join('package', 'LICENSE.md')); + expect(tree[0].children[3].getName()).toBe('bin'); + expect(tree[0].children[3].getPath()).toBe(path.join('package', 'bin')); + expect(tree[0].children[4].children[0].getName()).toBe('lister.js'); + expect(tree[0].children[4].children[0].getPath()).toBe(path.join('package', 'lib', 'lister.js')); + expect(tree[0].children[4].children[1].getName()).toBe('ls-archive-cli.js'); + expect(tree[0].children[4].children[1].getPath()).toBe(path.join('package', 'lib', 'ls-archive-cli.js')); + expect(tree[0].children[4].children[2].getName()).toBe('ls-archive.js'); + expect(tree[0].children[4].children[2].getPath()).toBe(path.join('package', 'lib', 'ls-archive.js')); + expect(tree[0].children[4].children[3].getName()).toBe('reader.js'); + expect(tree[0].children[4].children[3].getPath()).toBe(path.join('package', 'lib', 'reader.js')); + }); + })); + + describe("when the archive has multiple directories at the root", () => it("returns archive entries nested under their parent directory", function() { + let tree = null; + archive.list(path.join(__dirname, 'fixtures', 'nested.tar'), {tree: true}, (error, files) => tree = files); + waitsFor(() => tree != null); + runs(function() { + expect(tree.length).toBe(2); + + expect(tree[0].getPath()).toBe('d1'); + expect(tree[0].children[0].getName()).toBe('d2'); + expect(tree[0].children[0].children[0].getName()).toBe('d3'); + expect(tree[0].children[0].children[1].getName()).toBe('f1.txt'); + expect(tree[0].children[1].getName()).toBe('d4'); + expect(tree[0].children[2].getName()).toBe('f2.txt'); + + expect(tree[1].getPath()).toBe('da'); + expect(tree[1].children[0].getName()).toBe('db'); + expect(tree[1].children[1].getName()).toBe('fa.txt'); + }); + })); + }); + }); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + }); + + describe(".readFile()", function() { + describe("when the path exists in the archive", () => it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tar'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, 'file.txt', callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('hello\n')); + })); + + describe("when the path does not exist in the archive", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'one-file.tar'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.tar'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid tar file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.tar'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'invalid.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the path is a folder", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'one-folder.tar'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, `folder${path.sep}`, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + }); +}); diff --git a/spec/zip-spec.coffee b/spec/zip-spec.coffee deleted file mode 100644 index a387a74..0000000 --- a/spec/zip-spec.coffee +++ /dev/null @@ -1,127 +0,0 @@ -archive = require '../src/ls-archive' -path = require 'path' - -describe "zip files", -> - fixturesRoot = null - - beforeEach -> - fixturesRoot = path.join(__dirname, 'fixtures') - - describe ".list()", -> - describe "when the archive file exists", -> - it "returns files in the zip archive", -> - zipPaths = null - callback = (error, paths) -> zipPaths = paths - archive.list(path.join(fixturesRoot, 'one-file.zip'), callback) - waitsFor -> zipPaths? - runs -> - expect(zipPaths.length).toBe 1 - expect(zipPaths[0].path).toBe 'file.txt' - expect(zipPaths[0].isDirectory()).toBe false - expect(zipPaths[0].isFile()).toBe true - expect(zipPaths[0].isSymbolicLink()).toBe false - - it "returns folders in the zip archive", -> - zipPaths = null - callback = (error, paths) -> zipPaths = paths - archive.list(path.join(fixturesRoot, 'one-folder.zip'), callback) - waitsFor -> zipPaths? - runs -> - expect(zipPaths.length).toBe 1 - expect(zipPaths[0].path).toBe 'folder' - expect(zipPaths[0].isDirectory()).toBe true - expect(zipPaths[0].isFile()).toBe false - expect(zipPaths[0].isSymbolicLink()).toBe false - - describe "when the tree option is set to true", -> - it "returns archive entries nested under their parent directory", -> - tree = null - archive.list path.join(__dirname, 'fixtures', 'nested.zip'), tree: true, (error, files) -> - tree = files - waitsFor -> tree? - runs -> - expect(tree.length).toBe 2 - - expect(tree[0].getPath()).toBe 'd1' - expect(tree[0].children[0].getName()).toBe 'd2' - expect(tree[0].children[0].children[0].getName()).toBe 'd3' - expect(tree[0].children[0].children[1].getName()).toBe 'f1.txt' - expect(tree[0].children[1].getName()).toBe 'd4' - expect(tree[0].children[2].getName()).toBe 'f2.txt' - - expect(tree[1].getPath()).toBe 'da' - expect(tree[1].children[0].getName()).toBe 'db' - expect(tree[1].children[1].getName()).toBe 'fa.txt' - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.zip') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid zip file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.zip') - pathError = null - callback = (error) -> pathError = error - archive.list(archivePath, callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe ".readFile()", -> - describe "when the path exists in the archive", -> - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'one-file.zip') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, 'file.txt', callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe 'hello\n' - - describe "when the path does not exist in the archive", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'one-file.zip') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path does not exist", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'not-a-file.zip') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'not-a-file.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive path isn't a valid zip file", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'invalid.zip') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, 'invalid.txt', callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the path is a folder", -> - it "calls back with an error", -> - archivePath = path.join(fixturesRoot, 'one-folder.zip') - pathError = null - callback = (error, contents) -> pathError = error - archive.readFile(archivePath, "folder#{path.sep}", callback) - waitsFor -> pathError? - runs -> expect(pathError.message.length).toBeGreaterThan 0 - - describe "when the archive contains nested directories", -> - it "calls back with the contents of the given path", -> - archivePath = path.join(fixturesRoot, 'nested.zip') - pathContents = null - callback = (error, contents) -> pathContents = contents - archive.readFile(archivePath, "d1#{path.sep}d2#{path.sep}f1.txt", callback) - waitsFor -> pathContents? - runs -> expect(pathContents.toString()).toBe '' diff --git a/spec/zip-spec.js b/spec/zip-spec.js new file mode 100644 index 0000000..e380e4a --- /dev/null +++ b/spec/zip-spec.js @@ -0,0 +1,134 @@ +const archive = require('../src/ls-archive'); +const path = require('path'); + +describe("zip files", function() { + let fixturesRoot = null; + + beforeEach(() => fixturesRoot = path.join(__dirname, 'fixtures')); + + describe(".list()", function() { + describe("when the archive file exists", function() { + it("returns files in the zip archive", function() { + let zipPaths = null; + const callback = (error, paths) => zipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-file.zip'), callback); + waitsFor(() => zipPaths != null); + runs(function() { + expect(zipPaths.length).toBe(1); + expect(zipPaths[0].path).toBe('file.txt'); + expect(zipPaths[0].isDirectory()).toBe(false); + expect(zipPaths[0].isFile()).toBe(true); + expect(zipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + it("returns folders in the zip archive", function() { + let zipPaths = null; + const callback = (error, paths) => zipPaths = paths; + archive.list(path.join(fixturesRoot, 'one-folder.zip'), callback); + waitsFor(() => zipPaths != null); + runs(function() { + expect(zipPaths.length).toBe(1); + expect(zipPaths[0].path).toBe('folder'); + expect(zipPaths[0].isDirectory()).toBe(true); + expect(zipPaths[0].isFile()).toBe(false); + expect(zipPaths[0].isSymbolicLink()).toBe(false); + }); + }); + + describe("when the tree option is set to true", () => it("returns archive entries nested under their parent directory", function() { + let tree = null; + archive.list(path.join(__dirname, 'fixtures', 'nested.zip'), {tree: true}, (error, files) => tree = files); + waitsFor(() => tree != null); + runs(function() { + expect(tree.length).toBe(2); + + expect(tree[0].getPath()).toBe('d1'); + expect(tree[0].children[0].getName()).toBe('d2'); + expect(tree[0].children[0].children[0].getName()).toBe('d3'); + expect(tree[0].children[0].children[1].getName()).toBe('f1.txt'); + expect(tree[0].children[1].getName()).toBe('d4'); + expect(tree[0].children[2].getName()).toBe('f2.txt'); + + expect(tree[1].getPath()).toBe('da'); + expect(tree[1].children[0].getName()).toBe('db'); + expect(tree[1].children[1].getName()).toBe('fa.txt'); + }); + })); + }); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.zip'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid zip file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.zip'); + let pathError = null; + const callback = error => pathError = error; + archive.list(archivePath, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + }); + + describe(".readFile()", function() { + describe("when the path exists in the archive", () => it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'one-file.zip'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, 'file.txt', callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('hello\n')); + })); + + describe("when the path does not exist in the archive", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'one-file.zip'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path does not exist", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'not-a-file.zip'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'not-a-file.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive path isn't a valid zip file", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'invalid.zip'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, 'invalid.txt', callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the path is a folder", () => it("calls back with an error", function() { + const archivePath = path.join(fixturesRoot, 'one-folder.zip'); + let pathError = null; + const callback = (error, contents) => pathError = error; + archive.readFile(archivePath, `folder${path.sep}`, callback); + waitsFor(() => pathError != null); + runs(() => expect(pathError.message.length).toBeGreaterThan(0)); + })); + + describe("when the archive contains nested directories", () => it("calls back with the contents of the given path", function() { + const archivePath = path.join(fixturesRoot, 'nested.zip'); + let pathContents = null; + const callback = (error, contents) => pathContents = contents; + archive.readFile(archivePath, `d1${path.sep}d2${path.sep}f1.txt`, callback); + waitsFor(() => pathContents != null); + runs(() => expect(pathContents.toString()).toBe('')); + })); + }); +});