From 88e11347bc9bf24eaed302a34436a2c0c6ac0e75 Mon Sep 17 00:00:00 2001 From: Cameron Diver Date: Mon, 20 Aug 2018 13:04:06 +0100 Subject: [PATCH] tests: Add tests for ignore files Signed-off-by: Cameron Diver --- tests/utils/ignore.spec.coffee | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 tests/utils/ignore.spec.coffee diff --git a/tests/utils/ignore.spec.coffee b/tests/utils/ignore.spec.coffee new file mode 100644 index 00000000..c7c42de9 --- /dev/null +++ b/tests/utils/ignore.spec.coffee @@ -0,0 +1,85 @@ +require 'mocha' +chai = require 'chai' +_ = require 'lodash' +path = require('path') + +expect = chai.expect + +{ FileIgnorer, IgnoreFileType } = require '../../build/utils/ignore' + +describe 'File ignorer', -> + + it 'should detect ignore files', -> + f = new FileIgnorer('.' + path.sep) + expect(f.getIgnoreFileType('.gitignore')).to.equal(IgnoreFileType.GitIgnore) + expect(f.getIgnoreFileType('.dockerignore')).to.equal(IgnoreFileType.DockerIgnore) + expect(f.getIgnoreFileType('./.gitignore')).to.equal(IgnoreFileType.GitIgnore) + expect(f.getIgnoreFileType('./.dockerignore')).to.equal(IgnoreFileType.DockerIgnore) + + # gitignore files can appear in subdirectories, but dockerignore files cannot + expect(f.getIgnoreFileType('./subdir/.gitignore')).to.equal(IgnoreFileType.GitIgnore) + expect(f.getIgnoreFileType('./subdir/.dockerignore')).to.equal(null) + expect(f.getIgnoreFileType('./subdir/subdir2/.gitignore')).to.equal(IgnoreFileType.GitIgnore) + + expect(f.getIgnoreFileType('file')).to.equal(null) + expect(f.getIgnoreFileType('./file')).to.equal(null) + + it 'should filter files from the root directory', -> + + ignore = new FileIgnorer('.' + path.sep) + ignore.gitIgnoreEntries = [ + { pattern: '*.ignore', filePath: '.gitignore' } + ] + ignore.dockerIgnoreEntries = [ + { pattern: '*.ignore2', filePath: '.dockerignore' } + ] + files = [ + 'a' + 'a/b' + 'a/b/c' + 'file.ignore' + 'file2.ignore' + 'file.ignore2' + 'file2.ignore' + ] + + expect(_.filter(files, ignore.filter.bind(ignore))).to.deep.equal([ + 'a' + 'a/b' + 'a/b/c' + ]) + + it 'should filter files from subdirectories', -> + + ignore = new FileIgnorer('.' + path.sep) + ignore.gitIgnoreEntries = [ + { pattern: '*.ignore', filePath: 'lib/.gitignore' } + ] + files = [ + 'test.ignore' + 'root.ignore' + 'lib/normal-file' + 'lib/should.ignore' + 'lib/thistoo.ignore' + ] + expect(_.filter(files, ignore.filter.bind(ignore))).to.deep.equal([ + 'test.ignore' + 'root.ignore' + 'lib/normal-file' + ]) + + ignore.gitIgnoreEntries = [ + { pattern: '*.ignore', filePath: './lib/.gitignore' } + ] + files = [ + 'test.ignore' + 'root.ignore' + 'lib/normal-file' + 'lib/should.ignore' + 'lib/thistoo.ignore' + ] + expect(_.filter(files, ignore.filter.bind(ignore))).to.deep.equal([ + 'test.ignore' + 'root.ignore' + 'lib/normal-file' + ]) \ No newline at end of file