|
| 1 | +'use strict'; |
| 2 | +const {expect} = require('chai'); |
| 3 | +const proxyquire = require('proxyquire').noCallThru(); |
| 4 | +const sinon = require('sinon'); |
| 5 | +const stripAnsi = require('strip-ansi'); |
| 6 | +const os = require('os'); |
| 7 | +const fs = require('fs-extra'); |
| 8 | + |
| 9 | +function fake(stubs = {}) { |
| 10 | + return proxyquire('../../../lib/utils/pre-checks', stubs); |
| 11 | +} |
| 12 | + |
| 13 | +function getTasks(stubs = {}, ui = {}, system = {}) { |
| 14 | + const preChecks = fake(stubs); |
| 15 | + const listr = sinon.stub().resolves(); |
| 16 | + |
| 17 | + return preChecks(Object.assign({listr}, ui), system).then(() => { |
| 18 | + expect(listr.calledOnce).to.be.true; |
| 19 | + return listr.args[0][0]; |
| 20 | + }); |
| 21 | +} |
| 22 | + |
| 23 | +describe('Unit: Utils > pre-checks', function () { |
| 24 | + describe('update check', function () { |
| 25 | + it('rejects error if latestVersion has an error', function (done) { |
| 26 | + const pkg = {name: 'ghost', version: '1.0.0'}; |
| 27 | + const testError = new Error('update check'); |
| 28 | + const latestVersion = sinon.stub().rejects(testError); |
| 29 | + |
| 30 | + getTasks({ |
| 31 | + '../../package.json': pkg, |
| 32 | + 'latest-version': latestVersion |
| 33 | + }).then(([task]) => { |
| 34 | + expect(task.title).to.equal('Checking for Ghost-CLI updates'); |
| 35 | + |
| 36 | + return task.task(); |
| 37 | + }).catch((err) => { |
| 38 | + expect(err.message).to.equal(testError.message); |
| 39 | + expect(latestVersion.calledOnce).to.be.true; |
| 40 | + expect(latestVersion.calledWithExactly('ghost')).to.be.true; |
| 41 | + done(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('doesn\'t do anything if there are no updates', function () { |
| 46 | + const pkg = {name: 'ghost', version: '1.0.0'}; |
| 47 | + const latestVersion = sinon.stub().resolves('1.0.0'); |
| 48 | + const log = sinon.stub(); |
| 49 | + |
| 50 | + return getTasks({ |
| 51 | + '../../package.json': pkg, |
| 52 | + 'latest-version': latestVersion |
| 53 | + }, {log}).then(([task]) => task.task()).then(() => { |
| 54 | + expect(log.called).to.be.false; |
| 55 | + expect(latestVersion.calledOnce).to.be.true; |
| 56 | + expect(latestVersion.calledWithExactly('ghost')).to.be.true; |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('logs a message if an update is available', function () { |
| 61 | + const pkg = {name: 'ghost', version: '1.0.0'}; |
| 62 | + const latestVersion = sinon.stub().resolves('1.1.0'); |
| 63 | + const log = sinon.stub(); |
| 64 | + |
| 65 | + return getTasks({ |
| 66 | + '../../package.json': pkg, |
| 67 | + 'latest-version': latestVersion |
| 68 | + }, {log}).then(([task]) => task.task()).then(() => { |
| 69 | + expect(log.calledOnce).to.be.true; |
| 70 | + const msg = log.args[0][0]; |
| 71 | + |
| 72 | + expect(stripAnsi(msg)).to.match(/You are running an outdated version of Ghost-CLI/); |
| 73 | + |
| 74 | + expect(latestVersion.calledOnce).to.be.true; |
| 75 | + expect(latestVersion.calledWithExactly('ghost')).to.be.true; |
| 76 | + }); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe('~/.config folder ownership', function () { |
| 81 | + afterEach(() => { |
| 82 | + sinon.restore(); |
| 83 | + delete process.env.USER; |
| 84 | + }); |
| 85 | + |
| 86 | + it('rejects error if fs.lstat errors', function (done) { |
| 87 | + const homedir = sinon.stub(os, 'homedir').returns('/home/ghost'); |
| 88 | + const lstat = sinon.stub(fs, 'lstat').rejects(new Error('test error')); |
| 89 | + |
| 90 | + getTasks({}, {}, {platform: {linux: true}}).then(([,task]) => { |
| 91 | + expect(task.title).to.equal('Ensuring correct ~/.config folder ownership'); |
| 92 | + expect(task.isEnabled()).to.be.true; |
| 93 | + return task.task(); |
| 94 | + }).catch((error) => { |
| 95 | + expect(error.message).to.equal('test error'); |
| 96 | + expect(homedir.calledOnce).to.be.true; |
| 97 | + expect(lstat.calledOnce).to.be.true; |
| 98 | + expect(lstat.calledWithExactly('/home/ghost/.config')).to.be.true; |
| 99 | + done(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('doesn\'t do anything if directory ownership if fine', function () { |
| 104 | + sinon.stub(os, 'homedir').returns('/home/ghost'); |
| 105 | + sinon.stub(fs, 'lstat').resolves({uid: 1, gid: 1}); |
| 106 | + const uid = sinon.stub(process, 'getuid').returns(1); |
| 107 | + const gid = sinon.stub(process, 'getgid').returns(1); |
| 108 | + const sudo = sinon.stub().resolves(); |
| 109 | + |
| 110 | + return getTasks({}, {sudo}, {platform: {linux: false}}).then(([,task]) => { |
| 111 | + expect(task.isEnabled()).to.be.false; |
| 112 | + return task.task(); |
| 113 | + }).then(() => { |
| 114 | + expect(uid.calledOnce).to.be.true; |
| 115 | + expect(gid.calledOnce).to.be.true; |
| 116 | + expect(sudo.called).to.be.false; |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + it('calls chown if directory owner is not correct', function () { |
| 121 | + sinon.stub(os, 'homedir').returns('/home/ghost'); |
| 122 | + sinon.stub(fs, 'lstat').resolves({uid: 1, gid: 1}); |
| 123 | + const uid = sinon.stub(process, 'getuid').returns(2); |
| 124 | + const gid = sinon.stub(process, 'getgid').returns(2); |
| 125 | + const sudo = sinon.stub().resolves(); |
| 126 | + process.env.USER = 'ghostuser'; |
| 127 | + |
| 128 | + return getTasks({}, {sudo}).then(([,task]) => task.task()).then(() => { |
| 129 | + expect(uid.calledOnce).to.be.true; |
| 130 | + expect(gid.called).to.be.false; |
| 131 | + expect(sudo.calledOnce).to.be.true; |
| 132 | + |
| 133 | + expect(sudo.args[0][0]).to.equal('chown -R ghostuser:ghostuser /home/ghost/.config'); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it('calls chown if directory group is not correct', function () { |
| 138 | + sinon.stub(os, 'homedir').returns('/home/ghost'); |
| 139 | + sinon.stub(fs, 'lstat').resolves({uid: 2, gid: 1}); |
| 140 | + const uid = sinon.stub(process, 'getuid').returns(2); |
| 141 | + const gid = sinon.stub(process, 'getgid').returns(2); |
| 142 | + const sudo = sinon.stub().resolves(); |
| 143 | + process.env.USER = 'ghostuser'; |
| 144 | + |
| 145 | + return getTasks({}, {sudo}).then(([,task]) => task.task()).then(() => { |
| 146 | + expect(uid.calledOnce).to.be.true; |
| 147 | + expect(gid.calledOnce).to.be.true; |
| 148 | + expect(sudo.calledOnce).to.be.true; |
| 149 | + |
| 150 | + expect(sudo.args[0][0]).to.equal('chown -R ghostuser:ghostuser /home/ghost/.config'); |
| 151 | + }); |
| 152 | + }); |
| 153 | + }); |
| 154 | +}); |
0 commit comments