Skip to content

Commit

Permalink
fix: handle NaN-blocksize on windows file-system
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
nknapp committed Apr 5, 2018
1 parent 28691c3 commit 282d1fa
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/PackageStats.js
Expand Up @@ -37,7 +37,8 @@ class PackageStats {
totalBlockSize () {
if (!this._totalBlockSize) {
this._totalBlockSize = this.files.reduce((result, file) => {
return result + Math.ceil(file.stat.size / file.stat.blksize) * file.stat.blksize
const blksize = file.stat.blksize || 4096 // On NTFS, the blksize is NaN (#5). In order to get a result at all, we assume 4kb for NaN and 0.
return result + Math.ceil(file.stat.size / blksize) * blksize
}, 0)
}
return this._totalBlockSize
Expand Down
9 changes: 0 additions & 9 deletions test/DependencyTree-spec.js
Expand Up @@ -14,27 +14,22 @@ describe('The DependencyTree-class:', function () {
.then((tree) => expect(visit(tree.prod)).to.deep.equal([
{
'_id': '@scope/pkg@1.0.0',
'_size': 8192,
'deps': []
},
{
'_id': 'dep1@1.0.0',
'_size': 16384,
'deps': [
{
'_id': 'dep1a@1.0.0',
'_size': 8192,
'deps': []
}
]
},
{
'_id': 'dep2@1.0.0',
'_size': 16384,
'deps': [
{
'_id': 'dep2a@1.0.0',
'_size': 8192,
'deps': []
}
]
Expand All @@ -47,11 +42,9 @@ describe('The DependencyTree-class:', function () {
.then((tree) => expect(visit(tree.dev)).to.deep.equal([
{
'_id': 'devdep1@1.0.0',
'_size': 16384,
'deps': [
{
'_id': 'dep1a@1.0.0',
'_size': 8192,
'deps': []
}
]
Expand All @@ -64,7 +57,6 @@ describe('The DependencyTree-class:', function () {
.then((tree) => expect(visit(tree.manual)).to.deep.equal([
{
'_id': 'manualdep1@1.0.0',
'_size': 8192,
'deps': []
}
]))
Expand All @@ -76,7 +68,6 @@ function visit (pkgs) {
return pkgs.map((pkg) => {
return {
_id: pkg.packageJson._id,
_size: pkg.totalStats().totalBlockSize(),
deps: visit(pkg.dependencies)
}
})
Expand Down
36 changes: 22 additions & 14 deletions test/Package-spec.js
Expand Up @@ -6,6 +6,7 @@ const expect = chai.expect
const {Package} = require('../src/Package')
const {PackageStats} = require('../src/PackageStats')
const deep = require('deep-aplus')(Promise)
const path = require('path')

describe('The Package-class:', function () {
describe('The loadFrom static-method', function () {
Expand All @@ -14,13 +15,13 @@ describe('The Package-class:', function () {
.then((pkg) => {
expect(pkg.packageJson).to.deep.equal(require('./fixtures/project1/package.json'))
expect(pkg.stats.files.map((f) => f.file).sort()).to.deep.equal([
'test/fixtures/project1/',
'test/fixtures/project1/dir/',
'test/fixtures/project1/dir/file2.txt',
'test/fixtures/project1/file3.txt',
'test/fixtures/project1/file5000.txt',
'test/fixtures/project1/file6.txt',
'test/fixtures/project1/package.json'
f('test/fixtures/project1/'),
f('test/fixtures/project1/dir/'),
f('test/fixtures/project1/dir/file2.txt'),
f('test/fixtures/project1/file3.txt'),
f('test/fixtures/project1/file5000.txt'),
f('test/fixtures/project1/file6.txt'),
f('test/fixtures/project1/package.json')
])
})
})
Expand All @@ -30,13 +31,13 @@ describe('The Package-class:', function () {
.then((pkg) => {
expect(pkg.packageJson).to.deep.equal(require('./fixtures/project3/package.json'))
expect(pkg.stats.files.map((f) => f.file).sort()).to.deep.equal([
'test/fixtures/project3/',
'test/fixtures/project3/LICENSE.md',
'test/fixtures/project3/README',
'test/fixtures/project3/dir/',
'test/fixtures/project3/dir/file2.txt',
'test/fixtures/project3/file3.txt',
'test/fixtures/project3/package.json'
f('test/fixtures/project3/'),
f('test/fixtures/project3/LICENSE.md'),
f('test/fixtures/project3/README'),
f('test/fixtures/project3/dir/'),
f('test/fixtures/project3/dir/file2.txt'),
f('test/fixtures/project3/file3.txt'),
f('test/fixtures/project3/package.json')
])
})
})
Expand Down Expand Up @@ -177,3 +178,10 @@ describe('The Package-class:', function () {
function dummy (_id, _location, _requiredBy, stats) {
return new Package({_id, _location, _requiredBy}, stats || new PackageStats(`/${_id}`, []))
}

/**
* Normalizer file paths
*/
function f (file) {
return file.replace(/\//g, path.sep)
}
11 changes: 8 additions & 3 deletions test/PackageStats-spec.js
Expand Up @@ -5,6 +5,7 @@ chai.use(require('dirty-chai'))
const expect = chai.expect
const {PackageStats} = require('../src/PackageStats')
const fs = require('fs')
const path = require('path')

describe('The PackageStats-class', function () {
it('should load the package-stats from a directory', function () {
Expand Down Expand Up @@ -109,15 +110,18 @@ function sortedNameAndSize (files) {
* @return {string} file - size
*/
function f (file) {
return `${file} - ${fs.statSync(file).size}`
return `${file.replace(/\//g, path.sep)} - ${fs.statSync(file).size}`
}

/**
* Compute the bytesize-sum of all given files
* @param {...string} files a list of filenames
*/
function sumSize (...files) {
return files.reduce((sum, file) => sum + fs.statSync(file).size, 0)
return files.reduce((sum, file) => {
const size = fs.statSync(file).size
return sum + size
}, 0)
}

/**
Expand All @@ -127,6 +131,7 @@ function sumSize (...files) {
function sumBlocksize (...files) {
return files.reduce((sum, file) => {
const stat = fs.statSync(file)
return sum + Math.ceil(stat.size / stat.blksize) * stat.blksize
const blksize = stat.blksize || 4096 // On Windows, the blksize is NaN (#5). In order to get a result at all, we assume 4kb (default for NTFs) for NaN and 0.
return sum + Math.ceil(stat.size / blksize) * blksize
}, 0)
}
File renamed without changes.
6 changes: 6 additions & 0 deletions test/fixtures/moduleWithDeps-blk4096-win32.txt
@@ -0,0 +1,6 @@
size: 8k... with-dependencies: 28k
├─┬ dep1@1.0.0, 8k, 1 deps
│ └── dep1a@1.0.0, 4k, 0 deps
├─┬ dep2@1.0.0, 8k, 1 deps
│ └── dep2a@1.0.0, 4k, 0 deps
└── @scope/pkg@1.0.0, 4k, 0 deps
File renamed without changes.
4 changes: 4 additions & 0 deletions test/fixtures/moduleWithDeps-depth1-blk4096-win32.txt
@@ -0,0 +1,4 @@
size: 8k... with-dependencies: 28k
├── dep1@1.0.0, 8k, 1 deps
├── dep2@1.0.0, 8k, 1 deps
└── @scope/pkg@1.0.0, 4k, 0 deps
7 changes: 5 additions & 2 deletions test/index-spec.js
Expand Up @@ -12,17 +12,20 @@ const {analyze} = require('../src/index')
require('chalk').enabled = false

describe('The index-function (module main function):', function () {
// Output varies depending on the fs blocksize
const blksize = fs.statSync(__filename).blksize || 4096

it('should return an archy-tree for the dependencies of the referenced package directory', function () {
return analyze('test/fixtures/moduleWithDeps')
.then((result) => {
expect(result).to.equal(fs.readFileSync('test/fixtures/moduleWithDeps.txt', 'utf-8'))
expect(result).to.equal(fs.readFileSync(`test/fixtures/moduleWithDeps-blk${blksize}-${process.platform}.txt`, 'utf-8'))
})
})

it('should cut the display at a specific depth if specified', function () {
return analyze('test/fixtures/moduleWithDeps', {depth: 1})
.then((result) => {
expect(result).to.equal(fs.readFileSync('test/fixtures/moduleWithDeps-depth1.txt', 'utf-8'))
expect(result).to.equal(fs.readFileSync(`test/fixtures/moduleWithDeps-depth1-blk${blksize}-${process.platform}.txt`, 'utf-8'))
})
})
})

0 comments on commit 282d1fa

Please sign in to comment.