|
| 1 | +const t = require('tap') |
| 2 | +const path = require('path') |
| 3 | +const os = require('os') |
| 4 | +const fs = require('fs') |
| 5 | +const { open, stat } = require('fs/promises') |
| 6 | +const fswin = require('fswin') |
| 7 | +const sparse = require('..') |
| 8 | +const assert = require('assert'); |
| 9 | + |
| 10 | +const A_ONE = Buffer.from([1]) |
| 11 | +const TEN_MB = Buffer.alloc(100000000, 'helloworld') |
| 12 | +let i = 0 |
| 13 | + |
| 14 | +t.autoend(true) |
| 15 | + |
| 16 | +if(process.platform === 'win32') { |
| 17 | + |
| 18 | + t.test('basic functionality', async t => { |
| 19 | + let fd; // file descriptor |
| 20 | + |
| 21 | + const _path = t.testdir({}) |
| 22 | + const sparseFile = path.join(_path, 'sparse-file') |
| 23 | + |
| 24 | + sparse.setSparse(sparseFile, true) |
| 25 | + fd = await open(sparseFile, 'r+') |
| 26 | + await fd.write(TEN_MB, 0, TEN_MB.length, 0) |
| 27 | + fd.close() |
| 28 | + |
| 29 | + let fullsize = fswin.ntfs.getCompressedSizeSync(sparseFile) |
| 30 | + |
| 31 | + // will leave a file with a non-zero byte at either end. |
| 32 | + sparse.holePunch(sparseFile, 1, TEN_MB.length) |
| 33 | + |
| 34 | + let sparseSize = fswin.ntfs.getCompressedSizeSync(sparseFile) |
| 35 | + t.ok( |
| 36 | + sparseSize <= fullsize / 2, |
| 37 | + 'filesize should be significantly smaller\n' + |
| 38 | + `(sparse size: ${sparseSize} regular size: ${fullsize})` |
| 39 | + ) |
| 40 | + t.end() |
| 41 | + }) |
| 42 | + |
| 43 | + t.test('holepunch a basic file.', async t => { |
| 44 | + const _path=t.testdir({}) |
| 45 | + const basicFile = path.join(_path, 'basic-file') |
| 46 | + |
| 47 | + const fd = await open(basicFile, 'w+') |
| 48 | + await fd.write(TEN_MB, 0, TEN_MB.length, 0) |
| 49 | + fd.close() |
| 50 | + |
| 51 | + sparse.setSparse(basicFile) |
| 52 | + sparse.holePunch(basicFile, 1, TEN_MB.length) |
| 53 | + |
| 54 | + let stats = fswin.ntfs.getCompressedSizeSync(basicFile) |
| 55 | + t.ok(stats < TEN_MB.length, `file should be much smaller than 10mb (${stats})`) |
| 56 | + }) |
| 57 | + |
| 58 | + t.test('holepunch non-existant file.', async t => { |
| 59 | + const _path=t.testdir({}) |
| 60 | + const basicFile = path.join(_path, 'basic-file') |
| 61 | + |
| 62 | + t.notOk(sparse.holePunch(basicFile, 1, TEN_MB.length)) |
| 63 | + }) |
| 64 | + |
| 65 | + t.test('handle invalid cases like', async t => { |
| 66 | + const _path = t.testdir({ |
| 67 | + basic: 'helloworld' |
| 68 | + }) |
| 69 | + const file = path.join(_path, 'basic') |
| 70 | + |
| 71 | + t.test('wrong number of arguments', async t => { |
| 72 | + try { |
| 73 | + sparse.holePunch(file) |
| 74 | + t.fail('holePunch should throw with invalid parameters') |
| 75 | + } catch (err) { |
| 76 | + t.pass('ensure fails 1 arg') |
| 77 | + } |
| 78 | + try { |
| 79 | + sparse.holePunch(file, 0) |
| 80 | + t.fail('holePunch should throw with invalid parameters') |
| 81 | + } catch (err) { |
| 82 | + t.pass('ensure fails with 2 args') |
| 83 | + } |
| 84 | + }) |
| 85 | + t.test('negative numbers', async t => { |
| 86 | + try { |
| 87 | + sparse.holePunch(file, -10, 1000) |
| 88 | + t.fail('holePunch should throw with invalid parameters') |
| 89 | + } catch (err) { |
| 90 | + t.pass('ensure fails with negative numbers') |
| 91 | + } |
| 92 | + }) |
| 93 | + t.test('ranges outside of filesize', async t => { |
| 94 | + let stats = await stat(file) |
| 95 | + const size_before = stats.size |
| 96 | + assert(size_before < 999) |
| 97 | + sparse.holePunch(file, 0, 1000) |
| 98 | + stats = await stat(file) |
| 99 | + t.ok(stats.size === size_before, 'file should not get larger') |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | +} else { |
| 104 | + |
| 105 | + t.notOk(sparse.holePunch('./wont-be-created', 0, 1), 'On non-windows ensure no-op works') |
| 106 | + |
| 107 | +} |
0 commit comments