|
| 1 | +const glob = require('../src/glob') |
| 2 | +const os = require('os') |
| 3 | +const fs = require('fs') |
| 4 | + |
| 5 | +beforeEach(() => { |
| 6 | + os.tmpdir = jest.fn().mockImplementation(() => '/tmp') |
| 7 | + fs.mkdirSync('/tmp') |
| 8 | + fs.mkdirSync('/app_to_zip') |
| 9 | + fs.writeFileSync('/app_to_zip/.delete_me', '1') |
| 10 | + fs.writeFileSync('/app_to_zip/.pgb_dont_delete_me', '22') |
| 11 | + fs.writeFileSync('/app_to_zip/index.html', '333') |
| 12 | + fs.writeFileSync('/app_to_zip/cordova.js', '4444') |
| 13 | + fs.mkdirSync('/app_to_zip/res') |
| 14 | +}) |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + fs.rmdirSync('/tmp') |
| 18 | + fs.unlinkSync('/app_to_zip/.delete_me') |
| 19 | + fs.unlinkSync('/app_to_zip/.pgb_dont_delete_me') |
| 20 | + fs.unlinkSync('/app_to_zip/index.html') |
| 21 | + fs.unlinkSync('/app_to_zip/cordova.js') |
| 22 | + fs.rmdirSync('/app_to_zip/res') |
| 23 | + fs.rmdirSync('/app_to_zip') |
| 24 | +}) |
| 25 | + |
| 26 | +describe('glob', () => { |
| 27 | + test('should return files', () => { |
| 28 | + let result = { |
| 29 | + list: [ |
| 30 | + { 'path': '/app_to_zip/.pgb_dont_delete_me', 'size': 2 }, |
| 31 | + { 'path': '/app_to_zip/cordova.js', 'size': 4 }, |
| 32 | + { 'path': '/app_to_zip/index.html', 'size': 3 }, |
| 33 | + { 'path': '/app_to_zip/res', 'size': 0 } |
| 34 | + ], |
| 35 | + skipped: [ '/app_to_zip/.delete_me [HIDDEN]' ] |
| 36 | + } |
| 37 | + expect(glob.glob('/app_to_zip')).toEqual(result) |
| 38 | + }) |
| 39 | + |
| 40 | + test('should skip if file cant be read', () => { |
| 41 | + let error = new Error('bad file') |
| 42 | + error.code = 'ENOENT' |
| 43 | + let oldOpenSync = fs.openSync |
| 44 | + fs.openSync = jest.fn().mockImplementation(() => { throw error }) |
| 45 | + let result = { |
| 46 | + skipped: [ |
| 47 | + '/app_to_zip/.delete_me [HIDDEN]', |
| 48 | + '/app_to_zip/.pgb_dont_delete_me [ENOENT]', |
| 49 | + '/app_to_zip/cordova.js [ENOENT]', |
| 50 | + '/app_to_zip/index.html [ENOENT]', |
| 51 | + '/app_to_zip/res [ENOENT]' |
| 52 | + ], |
| 53 | + list: [ ] |
| 54 | + } |
| 55 | + expect(glob.glob('/app_to_zip')).toEqual(result) |
| 56 | + fs.openSync = oldOpenSync |
| 57 | + }) |
| 58 | + |
| 59 | + test('should skip ignored files', () => { |
| 60 | + let result = { |
| 61 | + list: [ |
| 62 | + { 'path': '/app_to_zip/.pgb_dont_delete_me', 'size': 2 }, |
| 63 | + { 'path': '/app_to_zip/cordova.js', 'size': 4 } |
| 64 | + ], |
| 65 | + skipped: [ '/app_to_zip/.delete_me [HIDDEN]', '/app_to_zip/index.html [IGNORED]', '/app_to_zip/res/ [IGNORED]' ] |
| 66 | + } |
| 67 | + expect(glob.glob('/app_to_zip', [ '**/*.html', 'res/' ])).toEqual(result) |
| 68 | + }) |
| 69 | +}) |
| 70 | + |
| 71 | +describe('toGlobRegex', () => { |
| 72 | + test('should skip ignored files', () => { |
| 73 | + expect(glob.toGlobRegex('')).toEqual(null) |
| 74 | + expect(glob.toGlobRegex(null)).toEqual(null) |
| 75 | + expect(glob.toGlobRegex('# comment')).toEqual(null) |
| 76 | + expect(glob.toGlobRegex('shell')).toEqual({ 'dir': false, 'negation': false, 'regex': /^shell\/?$/ }) |
| 77 | + expect(glob.toGlobRegex('dir/')).toEqual({ 'dir': true, 'negation': false, 'regex': /^dir\/?$/ }) |
| 78 | + expect(glob.toGlobRegex('dir/**')).toEqual({ 'dir': false, 'negation': false, 'regex': /^dir\/.*$/ }) |
| 79 | + expect(glob.toGlobRegex('/dir')).toEqual({ 'dir': false, 'negation': false, 'regex': /^dir\/?$/ }) |
| 80 | + expect(glob.toGlobRegex('!not_me')).toEqual({ 'dir': false, 'negation': true, 'regex': /^not_me\/?$/ }) |
| 81 | + expect(glob.toGlobRegex('(.moot?[]+)')).toEqual({ 'dir': false, 'negation': false, 'regex': /^\(\.moot.\[\]\+\)\/?$/ }) |
| 82 | + }) |
| 83 | +}) |
| 84 | + |
| 85 | +describe('filter', () => { |
| 86 | + test('should skip ignored files', () => { |
| 87 | + expect(glob.filter('', true, [])).toEqual(false) |
| 88 | + expect(glob.filter('', true, [null])).toEqual(false) |
| 89 | + expect(glob.filter('rabbit', true, [{ dir: true, negation: false, regex: /^rabbit\/?$/ }])).toEqual(true) |
| 90 | + expect(glob.filter('rabbit', true, [{ dir: false, negation: false, regex: /^rabbit\/?$/ }])).toEqual(true) |
| 91 | + expect(glob.filter('rabbit', true, [{ dir: true, negation: true, regex: /^rabbit$/ }])).toEqual(false) |
| 92 | + expect(glob.filter('rabbit', false, [{ dir: true, negation: false, regex: /^rabbit\/?$/ }])).toEqual(false) |
| 93 | + }) |
| 94 | +}) |
0 commit comments