-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
cache_spec.js
138 lines (106 loc) · 3.73 KB
/
cache_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require('../../spec_helper')
const mockfs = require('mock-fs')
const fs = require(`${lib}/fs`)
const state = require(`${lib}/tasks/state`)
const cache = require(`${lib}/tasks/cache`)
const stdout = require('../../support/stdout')
const snapshot = require('../../support/snapshot')
const moment = require('moment')
const stripAnsi = require('strip-ansi')
const path = require('path')
const termToHtml = require('term-to-html')
const outputHtmlFolder = path.join(__dirname, '..', '..', 'html')
describe('lib/tasks/cache', () => {
beforeEach(() => {
mockfs({
'/.cache/Cypress': {
'1.2.3': {
'Cypress': {},
},
'2.3.4': {
'Cypress.app': {},
},
},
})
sinon.stub(state, 'getCacheDir').returns('/.cache/Cypress')
sinon.stub(state, 'getBinaryDir').returns('/.cache/Cypress')
this.stdout = stdout.capture()
})
const getSnapshotText = () => {
this.stdout = this.stdout.toString().split('\n').slice(0, -1).join('\n')
const stdoutAsString = this.stdout.toString() || '[no output]'
// first restore the STDOUT, then confirm the value
// otherwise the error might not even appear or appear twice!
stdout.restore()
return stdoutAsString
}
const saveHtml = async (filename, html) => {
await fs.ensureDirAsync(outputHtmlFolder)
const htmlFilename = path.join(outputHtmlFolder, filename)
await fs.writeFileAsync(htmlFilename, html, 'utf8')
}
afterEach(() => {
mockfs.restore()
})
const defaultSnapshot = () => {
const stdoutAsString = getSnapshotText()
snapshot(stripAnsi(stdoutAsString))
}
const snapshotWithHtml = async (htmlFilename) => {
const stdoutAsString = getSnapshotText()
snapshot(stripAnsi(stdoutAsString))
// if the sanitized snapshot matches, let's save the ANSI colors converted into HTML
const html = termToHtml.strings(stdoutAsString, termToHtml.themes.dark.name)
await saveHtml(htmlFilename, html)
}
describe('.path', () => {
it('lists path to cache', () => {
cache.path()
expect(this.stdout.toString()).to.eql('/.cache/Cypress\n')
defaultSnapshot()
})
})
describe('.clear', () => {
it('deletes cache folder and everything inside it', () => {
return cache.clear()
.then(() => {
return fs.pathExistsAsync('/.cache/Cypress')
.then((exists) => {
expect(exists).to.eql(false)
defaultSnapshot()
})
})
})
})
describe('.list', () => {
it('lists all versions of cached binary', async function () {
// unknown access times
sinon.stub(state, 'getPathToExecutable').returns('/.cache/Cypress/1.2.3/app/cypress')
await cache.list()
defaultSnapshot()
})
it('lists all versions of cached binary with last access', async function () {
sinon.stub(state, 'getPathToExecutable').returns('/.cache/Cypress/1.2.3/app/cypress')
const statAsync = sinon.stub(fs, 'statAsync')
statAsync.onFirstCall().resolves({
atime: moment().subtract(3, 'month').valueOf(),
})
statAsync.onSecondCall().resolves({
atime: moment().subtract(5, 'day').valueOf(),
})
await cache.list()
await snapshotWithHtml('list-of-versions.html')
})
it('some versions have never been opened', async function () {
sinon.stub(state, 'getPathToExecutable').returns('/.cache/Cypress/1.2.3/app/cypress')
const statAsync = sinon.stub(fs, 'statAsync')
statAsync.onFirstCall().resolves({
atime: moment().subtract(3, 'month').valueOf(),
})
// the second binary has never been accessed
statAsync.onSecondCall().resolves()
await cache.list()
await snapshotWithHtml('second-binary-never-used.html')
})
})
})