Skip to content

Commit

Permalink
fix: do not sort when pruning snapshots, close #259
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jun 17, 2019
1 parent f31d636 commit 0c1c912
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
@@ -1,5 +1,5 @@
{
"editor.formatOnSave": true,
"standard.enable": true,
"workbench.colorTheme": "Abyss"
"workbench.colorTheme": "Activate SCARLET protocol (beta)"
}
13 changes: 9 additions & 4 deletions src/file-system.js
Expand Up @@ -161,12 +161,16 @@ function loadSnapshots (specFile, ext, opts = { useRelativePath: false }) {
function prepareFragments (snapshots, opts = { sortSnapshots: true }) {
la(isSaveOptions(opts), 'expected prepare fragments options', opts)

const names = opts.sortSnapshots
? Object.keys(snapshots).sort()
: Object.keys(snapshots)
const keys = Object.keys(snapshots)
debug(
'prepare %s, sorted? %d',
pluralize('snapshot', keys.length, true),
opts.sortSnapshots
)
const names = opts.sortSnapshots ? keys.sort() : keys

const fragments = names.map(testName => {
debug(`snapshot name "${testName}"`)
debug(`snapshot fragment name "${testName}"`)
const value = snapshots[testName]
const escapedName = escapeQuotes(testName)
return is.string(value)
Expand Down Expand Up @@ -209,6 +213,7 @@ function saveSnapshots (
debug('saving snapshots into %s for %s', filename, specRelativeName)
debug('snapshots are')
debug(snapshots)
debug('saveSnapshots options %o', opts)

return maybeSortAndSave(snapshots, filename, opts)
}
Expand Down
39 changes: 24 additions & 15 deletions src/prune.js
Expand Up @@ -21,34 +21,37 @@ const pruneSnapshotsInObject = (runtimeSnapshots, snapshots) => {

const keys = R.map(R.prop('key'), runtimeSnapshots)
debug(
'have runtime snapshots %s before pruning',
pluralize('name', keys.length, true)
'have runtime %s before pruning',
pluralize('snapshot name', keys.length, true)
)
if (debug.enabled) {
debug(keys.sort())
// make sure NOT to mutate the list of snapshot names
// otherwise we will save the pruned object with keys
// in the sorted order!
debug(keys)
debug('snapshot file keys in the current order')
debug(R.keys(snapshots))
}

// TODO simplify search, since now it is just an equality id:17
// - <https://github.com/bahmutov/snap-shot-core/issues/267>
// Gleb Bahmutov
// gleb.bahmutov@gmail.com
const isPresent = (val, key) => {
return R.find(k => key === k)(keys)
return R.includes(key, keys)
}
const prunedSnapshots = R.pickBy(isPresent, snapshots)
debug(
'after pruning remaining %s',
pluralize('name', R.keys(prunedSnapshots).length, true)
'after pruning %s remaining',
pluralize('snapshot name', R.keys(prunedSnapshots).length, true)
)
if (debug.enabled) {
debug(R.keys(prunedSnapshots).sort())
debug(R.keys(prunedSnapshots))
}

return prunedSnapshots
}

const pruneSnapshotsInFile = ({ fs, byFilename, ext }, opts) => file => {
const snapshots = fs.loadSnapshots(file, ext, opts)
const snapshots = fs.loadSnapshots(file, ext, {
useRelativePath: opts.useRelativePath || false
})
if (is.empty(snapshots)) {
debug('empty snapshots to prune in file', file)
return
Expand All @@ -65,7 +68,10 @@ const pruneSnapshotsInFile = ({ fs, byFilename, ext }, opts) => file => {
}

debug('saving pruned snapshot file for', file)
fs.saveSnapshots(file, prunedSnapshots, ext, opts)

const saveOptions = R.pick(['sortSnapshots', 'useRelativePath'], opts)
debug('save options %o', saveOptions)
fs.saveSnapshots(file, prunedSnapshots, ext, saveOptions)
}

// TODO switch to async id:3
Expand All @@ -77,15 +83,18 @@ const pruneSnapshotsInFile = ({ fs, byFilename, ext }, opts) => file => {
*/
const pruneSnapshots = fs => (
{ tests, ext = utils.DEFAULT_EXTENSION },
opts
opts = {
useRelativePath: false,
sortSnapshots: false
}
) => {
la(is.array(tests), 'missing tests', tests)
debug('pruning snapshots')
debug('run time tests')
debug(tests)

const byFilename = R.groupBy(R.prop('specFile'), tests)
debug('rune time tests by file')
debug('run-time tests by file')
debug(byFilename)

Object.keys(byFilename).forEach(
Expand Down

0 comments on commit 0c1c912

Please sign in to comment.