|
| 1 | +import path from 'path'; |
| 2 | +import test from 'ava'; |
| 3 | +import {outputJson, readJson, outputFile, readFile, pathExists} from 'fs-extra'; |
| 4 | +import tempy from 'tempy'; |
| 5 | +import execa from 'execa'; |
| 6 | +import {stub} from 'sinon'; |
| 7 | +import {WritableStreamBuffer} from 'stream-buffers'; |
| 8 | +import prepare from '../lib/prepare'; |
| 9 | + |
| 10 | +test.beforeEach(t => { |
| 11 | + t.context.log = stub(); |
| 12 | + t.context.logger = {log: t.context.log}; |
| 13 | + t.context.stdout = new WritableStreamBuffer(); |
| 14 | + t.context.stderr = new WritableStreamBuffer(); |
| 15 | +}); |
| 16 | + |
| 17 | +test('Updade package.json', async t => { |
| 18 | + const cwd = tempy.directory(); |
| 19 | + const packagePath = path.resolve(cwd, 'package.json'); |
| 20 | + await outputJson(packagePath, {version: '0.0.0-dev'}); |
| 21 | + |
| 22 | + await prepare( |
| 23 | + {}, |
| 24 | + { |
| 25 | + cwd, |
| 26 | + env: {}, |
| 27 | + stdout: t.context.stdout, |
| 28 | + stderr: t.context.stderr, |
| 29 | + nextRelease: {version: '1.0.0'}, |
| 30 | + logger: t.context.logger, |
| 31 | + } |
| 32 | + ); |
| 33 | + |
| 34 | + // Verify package.json has been updated |
| 35 | + t.is((await readJson(packagePath)).version, '1.0.0'); |
| 36 | + |
| 37 | + // Verify the logger has been called with the version updated |
| 38 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); |
| 39 | +}); |
| 40 | + |
| 41 | +test('Updade package.json and npm-shrinkwrap.json', async t => { |
| 42 | + const cwd = tempy.directory(); |
| 43 | + const packagePath = path.resolve(cwd, 'package.json'); |
| 44 | + const shrinkwrapPath = path.resolve(cwd, 'npm-shrinkwrap.json'); |
| 45 | + await outputJson(packagePath, {version: '0.0.0-dev'}); |
| 46 | + // Create a npm-shrinkwrap.json file |
| 47 | + await execa('npm', ['shrinkwrap'], {cwd}); |
| 48 | + |
| 49 | + await prepare( |
| 50 | + {}, |
| 51 | + { |
| 52 | + cwd, |
| 53 | + env: {}, |
| 54 | + stdout: t.context.stdout, |
| 55 | + stderr: t.context.stderr, |
| 56 | + nextRelease: {version: '1.0.0'}, |
| 57 | + logger: t.context.logger, |
| 58 | + } |
| 59 | + ); |
| 60 | + |
| 61 | + // Verify package.json and npm-shrinkwrap.json have been updated |
| 62 | + t.is((await readJson(packagePath)).version, '1.0.0'); |
| 63 | + t.is((await readJson(shrinkwrapPath)).version, '1.0.0'); |
| 64 | + // Verify the logger has been called with the version updated |
| 65 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); |
| 66 | +}); |
| 67 | + |
| 68 | +test('Updade package.json and package-lock.json', async t => { |
| 69 | + const cwd = tempy.directory(); |
| 70 | + const packagePath = path.resolve(cwd, 'package.json'); |
| 71 | + const packageLockPath = path.resolve(cwd, 'package-lock.json'); |
| 72 | + await outputJson(packagePath, {version: '0.0.0-dev'}); |
| 73 | + // Create a package-lock.json file |
| 74 | + await execa('npm', ['install'], {cwd}); |
| 75 | + |
| 76 | + await prepare( |
| 77 | + {}, |
| 78 | + { |
| 79 | + cwd, |
| 80 | + env: {}, |
| 81 | + stdout: t.context.stdout, |
| 82 | + stderr: t.context.stderr, |
| 83 | + nextRelease: {version: '1.0.0'}, |
| 84 | + logger: t.context.logger, |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + // Verify package.json and package-lock.json have been updated |
| 89 | + t.is((await readJson(packagePath)).version, '1.0.0'); |
| 90 | + t.is((await readJson(packageLockPath)).version, '1.0.0'); |
| 91 | + // Verify the logger has been called with the version updated |
| 92 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); |
| 93 | +}); |
| 94 | + |
| 95 | +test('Updade package.json and npm-shrinkwrap.json in a sub-directory', async t => { |
| 96 | + const cwd = tempy.directory(); |
| 97 | + const pkgRoot = 'dist'; |
| 98 | + const packagePath = path.resolve(cwd, pkgRoot, 'package.json'); |
| 99 | + const shrinkwrapPath = path.resolve(cwd, pkgRoot, 'npm-shrinkwrap.json'); |
| 100 | + await outputJson(packagePath, {version: '0.0.0-dev'}); |
| 101 | + // Create a npm-shrinkwrap.json file |
| 102 | + await execa('npm', ['shrinkwrap'], {cwd: path.resolve(cwd, pkgRoot)}); |
| 103 | + |
| 104 | + await prepare( |
| 105 | + {pkgRoot}, |
| 106 | + { |
| 107 | + cwd, |
| 108 | + env: {}, |
| 109 | + stdout: t.context.stdout, |
| 110 | + stderr: t.context.stderr, |
| 111 | + nextRelease: {version: '1.0.0'}, |
| 112 | + logger: t.context.logger, |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + // Verify package.json and npm-shrinkwrap.json have been updated |
| 117 | + t.is((await readJson(packagePath)).version, '1.0.0'); |
| 118 | + t.is((await readJson(shrinkwrapPath)).version, '1.0.0'); |
| 119 | + // Verify the logger has been called with the version updated |
| 120 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]); |
| 121 | +}); |
| 122 | + |
| 123 | +test('Updade package.json and package-lock.json in a sub-directory', async t => { |
| 124 | + const cwd = tempy.directory(); |
| 125 | + const pkgRoot = 'dist'; |
| 126 | + const packagePath = path.resolve(cwd, pkgRoot, 'package.json'); |
| 127 | + const packageLockPath = path.resolve(cwd, pkgRoot, 'package-lock.json'); |
| 128 | + await outputJson(packagePath, {version: '0.0.0-dev'}); |
| 129 | + // Create a package-lock.json file |
| 130 | + await execa('npm', ['install'], {cwd: path.resolve(cwd, pkgRoot)}); |
| 131 | + |
| 132 | + await prepare( |
| 133 | + {pkgRoot}, |
| 134 | + { |
| 135 | + cwd, |
| 136 | + env: {}, |
| 137 | + stdout: t.context.stdout, |
| 138 | + stderr: t.context.stderr, |
| 139 | + nextRelease: {version: '1.0.0'}, |
| 140 | + logger: t.context.logger, |
| 141 | + } |
| 142 | + ); |
| 143 | + |
| 144 | + // Verify package.json and package-lock.json have been updated |
| 145 | + t.is((await readJson(packagePath)).version, '1.0.0'); |
| 146 | + t.is((await readJson(packageLockPath)).version, '1.0.0'); |
| 147 | + // Verify the logger has been called with the version updated |
| 148 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', path.resolve(cwd, pkgRoot)]); |
| 149 | +}); |
| 150 | + |
| 151 | +test('Preserve indentation and newline', async t => { |
| 152 | + const cwd = tempy.directory(); |
| 153 | + const packagePath = path.resolve(cwd, 'package.json'); |
| 154 | + await outputFile(packagePath, `{\r\n "name": "package-name",\r\n "version": "0.0.0-dev"\r\n}\r\n`); |
| 155 | + |
| 156 | + await prepare( |
| 157 | + {}, |
| 158 | + { |
| 159 | + cwd, |
| 160 | + env: {}, |
| 161 | + stdout: t.context.stdout, |
| 162 | + stderr: t.context.stderr, |
| 163 | + nextRelease: {version: '1.0.0'}, |
| 164 | + logger: t.context.logger, |
| 165 | + } |
| 166 | + ); |
| 167 | + |
| 168 | + // Verify package.json has been updated |
| 169 | + t.is( |
| 170 | + await readFile(packagePath, 'utf-8'), |
| 171 | + `{\r\n "name": "package-name",\r\n "version": "1.0.0"\r\n}\r\n` |
| 172 | + ); |
| 173 | + |
| 174 | + // Verify the logger has been called with the version updated |
| 175 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); |
| 176 | +}); |
| 177 | + |
| 178 | +test('Use default indentation and newline if it cannot be detected', async t => { |
| 179 | + const cwd = tempy.directory(); |
| 180 | + const packagePath = path.resolve(cwd, 'package.json'); |
| 181 | + await outputFile(packagePath, `{"name": "package-name","version": "0.0.0-dev"}`); |
| 182 | + |
| 183 | + await prepare( |
| 184 | + {}, |
| 185 | + { |
| 186 | + cwd, |
| 187 | + env: {}, |
| 188 | + stdout: t.context.stdout, |
| 189 | + stderr: t.context.stderr, |
| 190 | + nextRelease: {version: '1.0.0'}, |
| 191 | + logger: t.context.logger, |
| 192 | + } |
| 193 | + ); |
| 194 | + |
| 195 | + // Verify package.json has been updated |
| 196 | + t.is(await readFile(packagePath, 'utf-8'), `{\n "name": "package-name",\n "version": "1.0.0"\n}\n`); |
| 197 | + |
| 198 | + // Verify the logger has been called with the version updated |
| 199 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); |
| 200 | +}); |
| 201 | + |
| 202 | +test('Create the package in the "tarballDir" directory', async t => { |
| 203 | + const cwd = tempy.directory(); |
| 204 | + const packagePath = path.resolve(cwd, 'package.json'); |
| 205 | + const pkg = {name: 'my-pkg', version: '0.0.0-dev'}; |
| 206 | + await outputJson(packagePath, pkg); |
| 207 | + |
| 208 | + await prepare( |
| 209 | + {tarballDir: 'tarball'}, |
| 210 | + { |
| 211 | + cwd, |
| 212 | + env: {}, |
| 213 | + stdout: t.context.stdout, |
| 214 | + stderr: t.context.stderr, |
| 215 | + nextRelease: {version: '1.0.0'}, |
| 216 | + logger: t.context.logger, |
| 217 | + } |
| 218 | + ); |
| 219 | + |
| 220 | + // Verify package.json has been updated |
| 221 | + t.is((await readJson(packagePath)).version, '1.0.0'); |
| 222 | + |
| 223 | + t.true(await pathExists(path.resolve(cwd, `tarball/${pkg.name}-1.0.0.tgz`))); |
| 224 | + // Verify the logger has been called with the version updated |
| 225 | + t.deepEqual(t.context.log.args[0], ['Write version %s to package.json in %s', '1.0.0', cwd]); |
| 226 | +}); |
0 commit comments