This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathtouch.js
126 lines (100 loc) · 3.19 KB
/
touch.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
/* eslint-env mocha */
'use strict'
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const delay = require('delay')
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.files.touch', function () {
this.timeout(10 * 1000)
let ipfs
async function testMtime (mtime, expectedMtime) {
const testPath = `/test-${hat()}`
await ipfs.files.write(testPath, Buffer.from('Hello, world!'), {
create: true
})
const stat = await ipfs.files.stat(testPath)
expect(stat).to.not.have.deep.property('mtime', expectedMtime)
await ipfs.files.touch(testPath, {
mtime
})
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.nested.deep.property('mtime', expectedMtime)
}
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
it('should have default mtime', async function () {
this.slow(5 * 1000)
const testPath = `/test-${hat()}`
await ipfs.files.write(testPath, Buffer.from('Hello, world!'), {
create: true
})
const stat = await ipfs.files.stat(testPath)
expect(stat).to.not.have.property('mtime')
await ipfs.files.touch(testPath)
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.property('mtime').that.does.not.deep.equal({
secs: 0,
nsecs: 0
})
})
it('should update file mtime', async function () {
this.slow(5 * 1000)
const testPath = `/test-${hat()}`
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
await ipfs.files.write(testPath, Buffer.from('Hello, world!'), {
create: true,
mtime
})
await delay(2000)
await ipfs.files.touch(testPath)
const stat = await ipfs.files.stat(testPath)
expect(stat).to.have.nested.property('mtime.secs').that.is.greaterThan(seconds)
})
it('should update directory mtime', async function () {
this.slow(5 * 1000)
const testPath = `/test-${hat()}`
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
await ipfs.files.mkdir(testPath, {
create: true,
mtime
})
await delay(2000)
await ipfs.files.touch(testPath)
const stat2 = await ipfs.files.stat(testPath)
expect(stat2).to.have.nested.property('mtime.secs').that.is.greaterThan(seconds)
})
it('should set mtime as Date', async function () {
await testMtime(new Date(5000), {
secs: 5,
nsecs: 0
})
})
it('should set mtime as { nsecs, secs }', async function () {
const mtime = {
secs: 5,
nsecs: 0
}
await testMtime(mtime, mtime)
})
it('should set mtime as timespec', async function () {
await testMtime({
Seconds: 5,
FractionalNanoseconds: 0
}, {
secs: 5,
nsecs: 0
})
})
it('should set mtime as hrtime', async function () {
const mtime = process.hrtime()
await testMtime(mtime, {
secs: mtime[0],
nsecs: mtime[1]
})
})
})
}