2020-02-29 23:40:39 +00:00
|
|
|
const childProcess = require('child_process')
|
|
|
|
const { readFileSync } = require('fs')
|
|
|
|
|
|
|
|
const mockFS = require('mock-fs')
|
|
|
|
|
|
|
|
jest.mock('child_process')
|
|
|
|
childProcess.spawnSync = jest.fn(() => ({ stderr: mockPackOutput }))
|
|
|
|
|
|
|
|
const {
|
2020-03-01 00:38:31 +00:00
|
|
|
MANIFEST_FILENAME,
|
2020-02-29 23:40:39 +00:00
|
|
|
convertSizeToBytes,
|
|
|
|
getCurrentPackageStats,
|
|
|
|
getPreviousPackageStats,
|
2020-03-01 00:38:31 +00:00
|
|
|
createOrUpdateManifest,
|
2020-02-29 23:40:39 +00:00
|
|
|
} = require('./helpers')
|
|
|
|
|
|
|
|
const mockPackageSize = '1.1 kB'
|
|
|
|
const mockUnpackedSize = '9000 kB'
|
|
|
|
|
|
|
|
const mockPackOutput = `
|
|
|
|
npm notice
|
|
|
|
npm notice 📦 footprint@0.0.0
|
|
|
|
npm notice === Tarball Contents ===
|
|
|
|
npm notice 732B package.json
|
|
|
|
npm notice 1.8kB dist/helpers.js
|
|
|
|
npm notice 1.9kB dist/index.js
|
|
|
|
npm notice === Tarball Details ===
|
|
|
|
npm notice name: footprint
|
|
|
|
npm notice version: 0.0.0
|
|
|
|
npm notice filename: footprint-0.0.0.tgz
|
|
|
|
npm notice package size: ${mockPackageSize}
|
|
|
|
npm notice unpacked size: ${mockUnpackedSize}
|
|
|
|
npm notice shasum: bdf33d471543cd8126338a82a27b16a9010b8dbd
|
|
|
|
npm notice integrity: sha512-ZZvTg9GVcJw8J[...]bkE0xlqQhlt4Q==
|
|
|
|
npm notice total files: 3
|
|
|
|
npm notice
|
|
|
|
`
|
|
|
|
describe('Helpers', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mockFS.restore()
|
|
|
|
jest.restoreAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterAll(mockFS.restore)
|
|
|
|
|
|
|
|
describe('Size string conversion', () => {
|
|
|
|
it.each`
|
|
|
|
sizeString | expectedValue
|
|
|
|
${'1 B'} | ${1}
|
|
|
|
${'1.1 B'} | ${1.1}
|
|
|
|
${'1 kB'} | ${1000}
|
|
|
|
${'1.1kB'} | ${1100}
|
|
|
|
${'1 mB'} | ${1000000}
|
|
|
|
${'1.1 mB'} | ${1100000}
|
|
|
|
`(
|
|
|
|
'converts $sizeString properly to $expectedValue bytes',
|
|
|
|
({ sizeString, expectedValue }) => {
|
|
|
|
expect(convertSizeToBytes(sizeString)).toEqual(expectedValue)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Current package statistics', () => {
|
|
|
|
it('constructs the current package report properly', () => {
|
|
|
|
const packageSizeBytes = 1100
|
|
|
|
const unpackedSizeBytes = 9000000
|
|
|
|
|
|
|
|
expect(getCurrentPackageStats()).toEqual({
|
|
|
|
packageSize: mockPackageSize,
|
|
|
|
packageSizeBytes,
|
|
|
|
unpackedSize: mockUnpackedSize,
|
|
|
|
unpackedSizeBytes,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('Previous package statistics', () => {
|
|
|
|
it('constructs the previous package report properly', () => {
|
|
|
|
const packageSize = '0.9 kB'
|
|
|
|
const packageSizeBytes = 900
|
|
|
|
const unpackedSize = '90 kB'
|
|
|
|
const unpackedSizeBytes = 90000
|
|
|
|
const limit = '1 kB'
|
|
|
|
const limitBytes = 1000
|
|
|
|
const mockReport = { packageSize, unpackedSize, limit }
|
2020-03-01 00:38:31 +00:00
|
|
|
mockFS({ [MANIFEST_FILENAME]: JSON.stringify(mockReport) })
|
2020-02-29 23:40:39 +00:00
|
|
|
|
|
|
|
expect(getPreviousPackageStats()).toEqual({
|
|
|
|
packageSize,
|
|
|
|
packageSizeBytes,
|
|
|
|
unpackedSize,
|
|
|
|
unpackedSizeBytes,
|
|
|
|
limit,
|
|
|
|
limitBytes,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
it('returns an empty manifest if it fails to reads the manifest file', () => {
|
2020-02-29 23:40:39 +00:00
|
|
|
mockFS({
|
2020-03-01 00:38:31 +00:00
|
|
|
[MANIFEST_FILENAME]: 'not valid JSON',
|
2020-02-29 23:40:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
expect(getPreviousPackageStats()).toEqual({})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
describe('Creating or updating the manifest', () => {
|
2020-02-29 23:40:39 +00:00
|
|
|
const currentStats = {
|
|
|
|
packageSize: '1 kB',
|
|
|
|
unpackedSize: '10 kB',
|
|
|
|
}
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
const previousManifest = {
|
2020-02-29 23:40:39 +00:00
|
|
|
limit: '2 kB',
|
|
|
|
packageSize: '1.5 kB',
|
|
|
|
}
|
2020-03-01 00:38:31 +00:00
|
|
|
it('creates a anifest from the current data if no previous data is provided', () => {
|
2020-02-29 23:40:39 +00:00
|
|
|
mockFS({})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
createOrUpdateManifest({ current: currentStats })
|
2020-02-29 23:40:39 +00:00
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
const writtenManifest = readFileSync(MANIFEST_FILENAME, {
|
2020-02-29 23:40:39 +00:00
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
expect(JSON.parse(writtenManifest)).toEqual({
|
2020-02-29 23:40:39 +00:00
|
|
|
packageSize: currentStats.packageSize,
|
|
|
|
unpackedSize: currentStats.unpackedSize,
|
|
|
|
limit: currentStats.packageSize,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
it('updates the previous manifest sizes if previous data exists', () => {
|
2020-02-29 23:40:39 +00:00
|
|
|
mockFS({
|
2020-03-01 00:38:31 +00:00
|
|
|
[MANIFEST_FILENAME]: JSON.stringify(previousManifest),
|
2020-02-29 23:40:39 +00:00
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
createOrUpdateManifest({
|
2020-02-29 23:40:39 +00:00
|
|
|
current: currentStats,
|
2020-03-01 00:38:31 +00:00
|
|
|
previous: previousManifest,
|
2020-02-29 23:40:39 +00:00
|
|
|
updateLimit: false,
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
const writtenManifest = readFileSync(MANIFEST_FILENAME, {
|
2020-02-29 23:40:39 +00:00
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
expect(JSON.parse(writtenManifest)).toEqual({
|
2020-02-29 23:40:39 +00:00
|
|
|
packageSize: currentStats.packageSize,
|
|
|
|
unpackedSize: currentStats.unpackedSize,
|
2020-03-01 00:38:31 +00:00
|
|
|
limit: previousManifest.limit,
|
2020-02-29 23:40:39 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
it('updates the previous manifest sizes and limit if previous data exists and updateLimit is set', () => {
|
2020-02-29 23:40:39 +00:00
|
|
|
mockFS({
|
2020-03-01 00:38:31 +00:00
|
|
|
[MANIFEST_FILENAME]: JSON.stringify(previousManifest),
|
2020-02-29 23:40:39 +00:00
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
createOrUpdateManifest({
|
2020-02-29 23:40:39 +00:00
|
|
|
current: currentStats,
|
2020-03-01 00:38:31 +00:00
|
|
|
previous: previousManifest,
|
2020-02-29 23:40:39 +00:00
|
|
|
updateLimit: true,
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
const writtenManifest = readFileSync(MANIFEST_FILENAME, {
|
2020-02-29 23:40:39 +00:00
|
|
|
encoding: 'utf-8',
|
|
|
|
})
|
|
|
|
|
2020-03-01 00:38:31 +00:00
|
|
|
expect(JSON.parse(writtenManifest)).toEqual({
|
2020-02-29 23:40:39 +00:00
|
|
|
packageSize: currentStats.packageSize,
|
|
|
|
unpackedSize: currentStats.unpackedSize,
|
|
|
|
limit: currentStats.packageSize,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|