Skip to content
This repository was archived by the owner on Oct 1, 2021. It is now read-only.

Commit 6715d0d

Browse files
committed
Sharness tests
1 parent 43d0066 commit 6715d0d

File tree

5 files changed

+75
-7
lines changed

5 files changed

+75
-7
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
docs
22

3-
test/test-repo-for*
43
.eslintrc
54

65
# Logs
@@ -26,3 +25,7 @@ dist
2625

2726
# Dependency directory
2827
node_modules
28+
29+
# Tests
30+
test/test-repo-for*
31+
test/sharness/tmp

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ async function main (args) {
2727
desc: 'Path to the IPFS repo',
2828
type: 'string'
2929
})
30+
.option('migrations', {
31+
desc: 'Path to folder with migrations. Default: bundled migrations',
32+
type: 'string'
33+
})
3034
.command(commands.migrate)
3135
.command(commands.status)
3236
.command(commands.add)

src/commands.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,32 @@ function reportingClosure (action) {
2828
process.stdout.write(`${chalk.green(`[${currentlyMigrated}/${totalToMigrate}]`)} Successfully ${action} ${chalk.bold(migration.version)}: ${migration.description}\n`)
2929
}
3030

31-
async function migrate ({ repoPath, to, dry, revertOk }) {
31+
async function migrate ({ repoPath, migrations, to, dry, revertOk }) {
3232
repoPath = repoPath || process.env.IPFS_PATH || path.join(os.homedir(), '.jsipfs')
33+
34+
// Import migrations if set
35+
if (migrations) {
36+
migrations = require(migrations)
37+
}
38+
3339
const version = await repoVersion.getVersion(repoPath)
40+
41+
let action
42+
if (dry) {
43+
action = 'loaded migration'
44+
} else {
45+
if (!to || (version <= to)) {
46+
action = 'migrated to version'
47+
} else {
48+
action = 'reverted version'
49+
}
50+
}
51+
3452
const options = {
53+
migrations: migrations,
3554
toVersion: to,
3655
ignoreLock: false,
37-
onProgress: reportingClosure(dry ? 'loaded migration' : 'migrated to version'),
56+
onProgress: reportingClosure(action),
3857
isDryRun: dry
3958
}
4059

@@ -47,11 +66,16 @@ async function migrate ({ repoPath, to, dry, revertOk }) {
4766
}
4867
}
4968

50-
async function status ({ repoPath }) {
69+
async function status ({ repoPath, migrations }) {
5170
repoPath = repoPath || process.env.IPFS_PATH || path.join(os.homedir(), '.jsipfs')
5271

72+
// Import migrations if set
73+
if (migrations) {
74+
migrations = require(migrations)
75+
}
76+
5377
const version = await repoVersion.getVersion(repoPath)
54-
const lastMigrationVersion = migrator.getLatestMigrationVersion()
78+
const lastMigrationVersion = migrator.getLatestMigrationVersion(migrations)
5579
const statusString =
5680
version < lastMigrationVersion ? chalk.yellow('There are migrations to be applied!') : chalk.green('Nothing to migrate!')
5781

@@ -68,7 +92,7 @@ async function getAuthor () {
6892
}
6993
}
7094

71-
async function add ({ repoPath, empty }) {
95+
async function add ({ empty }) {
7296
const newMigrationVersion = migrator.getLatestMigrationVersion() + 1
7397
const newMigrationFolder = path.join(__dirname, '..', 'migrations', 'migration-' + newMigrationVersion)
7498

test/sharness/t0010-basic-commands.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66

77
test_description="Test installation and some basic commands"
88

9+
# setup temp repo
10+
rm -rf ./tmp
11+
cp -r ../test-repo ./tmp
12+
IPFS_PATH=$(echo `pwd`/tmp)
13+
export IPFS_PATH
14+
915
. lib/test-lib.sh
1016

1117
test_expect_success "current dir is writable" '
@@ -17,7 +23,33 @@ test_expect_success "jsipfs-migrations version succeeds" '
1723
'
1824

1925
test_expect_success "jsipfs-migrations help succeeds" '
20-
jsipfs-migrations --help > help.txt
26+
jsipfs-migrations --help > help.txt &&
27+
grep "migrate" help.txt
28+
'
29+
30+
test_expect_success "jsipfs-migrations status shows migrations are needed" $'
31+
jsipfs-migrations status --migrations ../test/test-migrations > status.txt &&
32+
grep "There are migrations to be applied!" status.txt &&
33+
grep "Current repo version: 1" status.txt &&
34+
grep "Last migration\'s version: 2" status.txt
35+
'
36+
37+
test_expect_success "jsipfs-migrations successfully migrate to latest version" $'
38+
jsipfs-migrations migrate --migrations ../test/test-migrations > migrate.txt &&
39+
grep "Successfully migrated to version 2" migrate.txt
2140
'
2241

42+
test_expect_success "jsipfs-migrations status shows NO migrations are needed" $'
43+
jsipfs-migrations status --migrations ../test/test-migrations > status.txt &&
44+
grep "Nothing to migrate!" status.txt &&
45+
grep "Current repo version: 2" status.txt &&
46+
grep "Last migration\'s version: 2" status.txt
47+
'
48+
49+
test_expect_success "jsipfs-migrations successfully reverts" $'
50+
jsipfs-migrations migrate --to 1 --revert-ok --migrations ../test/test-migrations > revert.txt &&
51+
grep "Successfully reverted version 2" revert.txt
52+
'
53+
54+
2355
test_done

0 commit comments

Comments
 (0)