check-disk-space 2.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test.js DELETED
@@ -1,205 +0,0 @@
1
- 'use strict'
2
-
3
- import test from 'ava'
4
-
5
- const platformFixtures = {
6
- win32: [
7
- {
8
- title: 'Windows XP',
9
- path: 'D:/a/long/path',
10
- execOutput: `
11
- Caption FreeSpace Size
12
- C: 627703808 10725732352
13
- D: 0 699783168
14
- E: 25851797504 107496861696
15
- F: 834660716544 2000396742656
16
- `,
17
- result: {
18
- diskPath: 'D:',
19
- free: 0,
20
- size: 699783168
21
- }
22
- },
23
- {
24
- title: 'Windows 7',
25
- path: 'C:/blah',
26
- execOutput: `
27
- Caption FreeSpace Size
28
- C: 1286164480 34359734272
29
- D: 1864638464 50925137920
30
- E:
31
- F: 77553082368 990202818560
32
- G:
33
- L:
34
- `,
35
- result: {
36
- diskPath: 'C:',
37
- free: 1286164480,
38
- size: 34359734272
39
- }
40
- },
41
- {
42
- title: 'Windows 8',
43
- path: 'D:/MyGames',
44
- execOutput: `
45
- Caption FreeSpace Size
46
- C: 498562174976 539028877312
47
- D: 0 59494400
48
- `,
49
- result: {
50
- diskPath: 'D:',
51
- free: 0,
52
- size: 59494400
53
- }
54
- },
55
- {
56
- title: 'Windows 10',
57
- path: 'C:/User/toto',
58
- execOutput: `
59
- Caption FreeSpace Size
60
- C: 159345410048 171204145152
61
- D: 0 4001759232
62
- `,
63
- result: {
64
- diskPath: 'C:',
65
- free: 159345410048,
66
- size: 171204145152
67
- }
68
- }
69
- ],
70
- linux: [
71
- {
72
- title: 'bananian',
73
- path: '/dev',
74
- execOutput: `
75
- Filesystem 1K-blocks Used Available Use% Mounted on
76
- devtmpfs 447624 0 447624 0% /dev
77
- `,
78
- result: {
79
- diskPath: '/dev',
80
- free: 447624 * 1024,
81
- size: 447624 * 1024
82
- }
83
- },
84
- {
85
- title: 'Ubuntu',
86
- path: '/run/user/1000',
87
- execOutput: `
88
- Filesystem 1024-blocks Used Available Capacity Mounted on
89
- tmpfs 89884 8 89876 1% /run/user/1000
90
- `,
91
- result: {
92
- diskPath: '/run/user/1000',
93
- free: 89876 * 1024,
94
- size: 89884 * 1024
95
- }
96
- },
97
- {
98
- title: 'Ubuntu: russian locale',
99
- path: '/media/Games',
100
- execOutput: `
101
- Файл.система 1024-блоков Использовано Доступно Вместимость Cмонтировано в
102
- /dev/sdc 240234168 188729228 39278628 83% /media/Games
103
- `,
104
- result: {
105
- diskPath: '/media/Games',
106
- free: 39278628 * 1024,
107
- size: 240234168 * 1024
108
- }
109
- }
110
- ],
111
- darwin: [
112
- {
113
- title: 'macOS',
114
- path: '/home/jacquie',
115
- execOutput: `
116
- Filesystem 1024-blocks Used Available Capacity Mounted on
117
- /dev/disk0s2 145961032 20678788 125026244 15% /home
118
- `,
119
- result: {
120
- diskPath: '/home',
121
- free: 125026244 * 1024,
122
- size: 145961032 * 1024
123
- }
124
- }
125
- ]
126
- }
127
-
128
- function mockCheckDiskSpace(platform, fixture) {
129
- // Clean some required paths
130
- delete require.cache[require.resolve('.')]
131
-
132
- // Forces the platform for test purposes
133
- Object.defineProperty(process, 'platform', {value: platform})
134
-
135
- // Mock child_process.exec
136
- require('child_process').exec = (cmd, callback) => {
137
- process.nextTick(() => {
138
- callback(fixture.execError, fixture.execOutput)
139
- })
140
- }
141
-
142
- return require('.')
143
- }
144
-
145
- function mockGetFirstExistingParentPath(existingDirectoryPath) {
146
- // Clean some required paths
147
- delete require.cache[require.resolve('.')]
148
-
149
- // Forces the platform for test purposes
150
- Object.defineProperty(process, 'platform', {value: 'linux'})
151
-
152
- // Mock child_process.exec
153
- require('fs').existsSync = directoryPath => directoryPath === existingDirectoryPath
154
-
155
- return require('.').getFirstExistingParentPath
156
- }
157
-
158
- Object.keys(platformFixtures).forEach(platform => {
159
- platformFixtures[platform].forEach(fixture => {
160
- test(`${platform}: ${fixture.title}`, async t => {
161
- const checkDiskSpace = mockCheckDiskSpace(platform, fixture)
162
-
163
- t.deepEqual(await checkDiskSpace(fixture.path), fixture.result)
164
- })
165
- })
166
- })
167
-
168
- test('win32: path did not match any disk', async t => {
169
- const checkDiskSpace = mockCheckDiskSpace('win32', platformFixtures.win32[0])
170
- const error = await t.throwsAsync(checkDiskSpace('Z:/shouldfail'))
171
- t.is(error.name, 'NoMatchError')
172
- })
173
-
174
- test('win32: invalid path', async t => {
175
- const checkDiskSpace = mockCheckDiskSpace('win32', platformFixtures.win32[0])
176
- const error = await t.throwsAsync(checkDiskSpace('an invalid path'))
177
- t.is(error.name, 'InvalidPathError')
178
- })
179
-
180
- test('unix: invalid path', async t => {
181
- const checkDiskSpace = mockCheckDiskSpace('linux', platformFixtures.linux[0])
182
- const error = await t.throwsAsync(checkDiskSpace('an invalid path'))
183
- t.is(error.name, 'InvalidPathError')
184
- })
185
-
186
- test('exec has an error', async t => {
187
- const fixture = JSON.parse(JSON.stringify(platformFixtures.win32[0]))
188
- fixture.execError = new Error('some error')
189
-
190
- const checkDiskSpace = mockCheckDiskSpace('win32', fixture)
191
- const error = await t.throwsAsync(checkDiskSpace('C:/something'))
192
- t.is(error.message, 'some error')
193
- })
194
-
195
- test('unix: get first existing parent path', t => {
196
- const parentPath = '/home/Lisa'
197
- const getFirstExistingParentPath = mockGetFirstExistingParentPath(parentPath)
198
- t.is(getFirstExistingParentPath('/home/Lisa/games/Ankama/Dofus'), parentPath)
199
- })
200
-
201
- test('unix: get first parent can be the path itself', t => {
202
- const parentPath = '/home/Lisa'
203
- const getFirstExistingParentPath = mockGetFirstExistingParentPath(parentPath)
204
- t.is(getFirstExistingParentPath(parentPath), parentPath)
205
- })