|
| 1 | +var childProcess = require('child_process') |
| 2 | +var fs = require('fs') |
| 3 | +var path = require('path') |
| 4 | + |
| 5 | +var tap = require('tap') |
| 6 | + |
| 7 | +var readJson = require('../') |
| 8 | + |
| 9 | +var isGit |
| 10 | +try { |
| 11 | + fs.readFileSync(path.resolve(__dirname, '../.git/HEAD')) |
| 12 | + isGit = true |
| 13 | +} catch (e) { |
| 14 | + isGit = false |
| 15 | +} |
| 16 | + |
| 17 | +if (isGit) { |
| 18 | + tap.test('gitHead tests', function (t) { |
| 19 | + t.plan(3) |
| 20 | + |
| 21 | + const repoProjectName = 'read-package-json' |
| 22 | + const repo = 'https://github.com/npm/' + repoProjectName + '.git' |
| 23 | + var repoDirs = [] |
| 24 | + |
| 25 | + t.test('detached case', function (tt) { |
| 26 | + var p = path.resolve(__dirname, '..', 'package.json') |
| 27 | + readJson(p, function (er, data) { |
| 28 | + if (er) throw er |
| 29 | + tt.ok(data) |
| 30 | + tt.similar(data.gitHead, /^[a-f0-9]{40}$/) |
| 31 | + tt.end() |
| 32 | + }) |
| 33 | + }) |
| 34 | + |
| 35 | + function testGitRepo (kind, extraRepoCommand, t) { |
| 36 | + var repoDirName = repoProjectName + '-' + kind |
| 37 | + var cmd = `cd ${__dirname} && git clone ${repo} ${repoDirName} && cd ${repoDirName}` |
| 38 | + if (extraRepoCommand) cmd += ` && ${extraRepoCommand}` |
| 39 | + childProcess.execSync(cmd) |
| 40 | + repoDirs.push(repoDirName) |
| 41 | + var p = path.resolve(__dirname, repoDirName, 'package.json') |
| 42 | + readJson(p, function (er, data) { |
| 43 | + if (er) throw er |
| 44 | + t.ok(data) |
| 45 | + t.similar(data.gitHead, /^[a-f0-9]{40}$/) |
| 46 | + t.end() |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + t.test('basic case', function (tt) { |
| 51 | + testGitRepo('basic', '', tt) |
| 52 | + }) |
| 53 | + |
| 54 | + t.test('git-pack-refs vs gitHead', function (tt) { |
| 55 | + testGitRepo('git-pack-refs', 'git pack-refs --all', tt) |
| 56 | + }) |
| 57 | + |
| 58 | + t.tearDown(function () { |
| 59 | + repoDirs.forEach(function (d) { |
| 60 | + childProcess.execSync(`rm -rf ${path.resolve(__dirname, d)}`) |
| 61 | + }) |
| 62 | + }) |
| 63 | + }) |
| 64 | +} |
0 commit comments