1
|
|
|
import chai from 'chai' |
2
|
|
|
import path from 'path' |
3
|
|
|
import sinonChai from'sinon-chai' |
4
|
|
|
chai.use(sinonChai) |
5
|
|
|
import sinon from 'sinon' |
6
|
|
|
import mkdirp from 'mkdirp' |
7
|
|
|
import fse from 'fs-extra' |
8
|
|
|
|
9
|
|
|
import { |
10
|
|
|
coreUtils, |
11
|
|
|
abeExtend, |
|
|
|
|
12
|
|
|
cmsData, |
13
|
|
|
config |
14
|
|
|
} from '../../../src/cli' |
15
|
|
|
config.set({root: path.join(process.cwd(), 'test', 'fixtures')}) |
16
|
|
|
|
17
|
|
|
describe('coreUtils.file', function() { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* coreUtils.file.addFolder |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
it('coreUtils.file.addFolder()', function() { |
24
|
|
|
this.sinon = sinon.sandbox.create(); |
25
|
|
|
var stub = sinon.stub(mkdirp, 'mkdirP') |
26
|
|
|
stub.returns({'test':'test'}); |
27
|
|
|
var result = coreUtils.file.addFolder('path/to/folder') |
28
|
|
|
chai.expect(result).to.be.a('string') |
29
|
|
|
chai.expect(result).to.equal('path/to/folder') |
30
|
|
|
mkdirp.mkdirP.restore() |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* coreUtils.file.removeFolder |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
|
|
it('coreUtils.file.removeFolder()', function() { |
38
|
|
|
this.sinon = sinon.sandbox.create(); |
39
|
|
|
var stub = sinon.stub(fse, 'remove') |
40
|
|
|
stub.returns({}); |
41
|
|
|
var result = coreUtils.file.removeFolder('path/to/folder') |
42
|
|
|
chai.expect(result).to.be.a('string') |
43
|
|
|
chai.expect(result).to.equal('path/to/folder') |
44
|
|
|
fse.remove.restore() |
45
|
|
|
}); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* coreUtils.file.getDate |
49
|
|
|
* |
50
|
|
|
*/ |
51
|
|
|
it('coreUtils.file.getDate()', function() { |
52
|
|
|
this.sinon = sinon.sandbox.create(); |
53
|
|
|
var stub = sinon.stub(cmsData.fileAttr, 'get') |
54
|
|
|
stub.returns({d: '2016-12-07T13:04:18.810Z'}); |
55
|
|
|
var result = coreUtils.file.getDate(path.join(config.root, config.data.url, 'article-abe-d20161207T130418810Z.json')) |
56
|
|
|
chai.expect(result.getYear()).to.equal(116) |
57
|
|
|
cmsData.fileAttr.get.restore() |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* coreUtils.file.addDateIsoToRevisionPath |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
it('coreUtils.file.addDateIsoToRevisionPath()', function() { |
65
|
|
|
var date = '20161207T132049118Z' |
66
|
|
|
var urlRevision = path.join(config.root, config.data.url, 'article-abe-d' + date + '.json') |
67
|
|
|
this.sinon = sinon.sandbox.create(); |
68
|
|
|
|
69
|
|
|
var stub = sinon.stub(cmsData.revision, 'removeStatusAndDateFromFileName') |
70
|
|
|
stub.returns(date); |
71
|
|
|
|
72
|
|
|
var stub2 = sinon.stub(cmsData.fileAttr, 'add') |
73
|
|
|
stub2.returns(urlRevision); |
74
|
|
|
|
75
|
|
|
var result = coreUtils.file.addDateIsoToRevisionPath(path.join(config.root, config.data.url, 'article.json'), 'draft') |
76
|
|
|
chai.expect(result).to.be.a('string') |
77
|
|
|
chai.expect(result).to.equal(urlRevision) |
78
|
|
|
cmsData.revision.removeStatusAndDateFromFileName.restore() |
79
|
|
|
cmsData.fileAttr.add.restore() |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* coreUtils.file.exist |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
|
it('coreUtils.file.exist()', function() { |
87
|
|
|
this.sinon = sinon.sandbox.create(); |
88
|
|
|
var stub = sinon.stub(fse, 'statSync') |
89
|
|
|
stub.returns(''); |
90
|
|
|
var result = coreUtils.file.exist('path/to/file') |
91
|
|
|
chai.expect(result).to.be.true |
|
|
|
|
92
|
|
|
fse.statSync.restore() |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
}); |
96
|
|
|
|