1
1
/* eslint-env mocha */
2
2
'use strict'
3
3
4
+ const os = require ( 'os' )
5
+ const fs = require ( 'fs' ) . promises
6
+ const path = require ( 'path' )
7
+ const hat = require ( 'hat' )
4
8
const { expect } = require ( 'interface-ipfs-core/src/utils/mocha' )
9
+ const { repoVersion } = require ( 'ipfs-repo' )
10
+ const promisify = require ( 'promisify-es6' )
11
+ const ncp = promisify ( require ( 'ncp' ) . ncp )
5
12
const runOnAndOff = require ( '../utils/on-and-off' )
13
+ const ipfsExec = require ( '../utils/ipfs-exec' )
14
+ const clean = require ( '../utils/clean' )
6
15
7
16
describe ( 'general cli options' , ( ) => runOnAndOff . off ( ( thing ) => {
8
17
it ( 'should handle --silent flag' , async ( ) => {
@@ -17,3 +26,83 @@ describe('general cli options', () => runOnAndOff.off((thing) => {
17
26
expect ( out ) . to . include ( 'again' )
18
27
} )
19
28
} ) )
29
+
30
+ describe ( '--migrate' , ( ) => {
31
+ let ipfs , repoPath
32
+
33
+ async function setRepoVersion ( version ) {
34
+ await fs . writeFile ( path . join ( repoPath , 'version' ) , version )
35
+ }
36
+
37
+ async function getRepoVersion ( ) {
38
+ return parseInt ( await fs . readFile ( path . join ( repoPath , 'version' ) , 'utf8' ) )
39
+ }
40
+
41
+ beforeEach ( async ( ) => {
42
+ repoPath = path . join ( os . tmpdir ( ) , `ipfs-${ hat ( ) } ` )
43
+ const v7RepoPath = path . join ( __dirname , '../fixtures/v7-repo' )
44
+ await ncp ( v7RepoPath , repoPath )
45
+ ipfs = ipfsExec ( repoPath )
46
+ } )
47
+
48
+ afterEach ( ( ) => clean ( repoPath ) )
49
+
50
+ it ( 'should not migrate for daemon command when --migrate flag not set' , async ( ) => {
51
+ // There are no migrations prior to 7 so it's safe to set version to 5 since
52
+ // the repo is the same. We set to 5 because version 6 & 7 are considered
53
+ // the same in repo.version.check.
54
+ await setRepoVersion ( 5 )
55
+ const err = await ipfs . fail ( 'daemon' )
56
+ expect ( err . stdout ) . to . include ( 'Pass --migrate for automatic migration' )
57
+ const version = await getRepoVersion ( )
58
+ expect ( version ) . to . equal ( 5 ) // Should not have migrated
59
+ } )
60
+
61
+ it ( 'should not migrate for other commands when --migrate flag not set' , async ( ) => {
62
+ // There are no migrations prior to 7 so it's safe to set version to 5 since
63
+ // the repo is the same. We set to 5 because version 6 & 7 are considered
64
+ // the same in repo.version.check.
65
+ await setRepoVersion ( 5 )
66
+ const err = await ipfs . fail ( 'files ls' )
67
+ expect ( err . stdout ) . to . include ( 'Pass --migrate for automatic migration' )
68
+ const version = await getRepoVersion ( )
69
+ expect ( version ) . to . equal ( 5 ) // Should not have migrated
70
+ } )
71
+
72
+ it ( 'should migrate for daemon command when --migrate flag set' , async ( ) => {
73
+ // There are no migrations prior to 7 so it's safe to set version to 5 since
74
+ // the repo is the same. We set to 5 because version 6 & 7 are considered
75
+ // the same in repo.version.check.
76
+ await setRepoVersion ( 5 )
77
+
78
+ const daemon = ipfs ( 'daemon --migrate' )
79
+ let stdout = ''
80
+
81
+ daemon . stdout . on ( 'data' , data => {
82
+ stdout += data . toString ( 'utf8' )
83
+
84
+ if ( stdout . includes ( 'Daemon is ready' ) ) {
85
+ daemon . kill ( )
86
+ }
87
+ } )
88
+
89
+ await expect ( daemon )
90
+ . to . eventually . be . rejected ( )
91
+ . and . to . include ( {
92
+ killed : true
93
+ } )
94
+
95
+ const version = await getRepoVersion ( )
96
+ expect ( version ) . to . equal ( repoVersion ) // Should have migrated to latest
97
+ } )
98
+
99
+ it ( 'should migrate for other commands when --migrate flag set' , async ( ) => {
100
+ // There are no migrations prior to 7 so it's safe to set version to 5 since
101
+ // the repo is the same. We set to 5 because version 6 & 7 are considered
102
+ // the same in repo.version.check.
103
+ await setRepoVersion ( 5 )
104
+ await ipfs ( 'files ls --migrate' )
105
+ const version = await getRepoVersion ( )
106
+ expect ( version ) . to . equal ( repoVersion ) // Should have migrated to latest
107
+ } )
108
+ } )
0 commit comments