|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Completion = require('../lib/completion'); |
| 4 | +const { UnsupportedShellError, CompletionScriptError } = require('../lib/errors'); |
| 5 | +const sinon = require('sinon'); |
| 6 | +const fs = require('fs'); |
| 7 | +const os = require('os'); |
| 8 | +const should = require('should'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +describe('Completion', () => { |
| 12 | + const zshrcPath = path.join(os.homedir(), '.zshrc'); |
| 13 | + const bashrcPath = path.join(os.homedir(), '.bashrc'); |
| 14 | + |
| 15 | + describe('constructor()', () => { |
| 16 | + it('should construct with supported shell', () => { |
| 17 | + const completion = new Completion('zsh'); |
| 18 | + should.exist(completion); |
| 19 | + completion.shell.should.equal('zsh'); |
| 20 | + completion.rcFilename.should.equal('.zshrc'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should throw UnsupportedShellError for unsupported shell', () => { |
| 24 | + (() => {return new Completion('fish');}).should.throw(UnsupportedShellError); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('getFilePath()', () => { |
| 29 | + it('should return .zshrc path for zsh', () => { |
| 30 | + const completion = new Completion('zsh'); |
| 31 | + completion.getFilePath().should.equal(zshrcPath); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return .bashrc path for bash', () => { |
| 35 | + const completion = new Completion('bash'); |
| 36 | + completion.getFilePath().should.equal(bashrcPath); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('appendScript()', () => { |
| 41 | + let appendFileStub; |
| 42 | + |
| 43 | + beforeEach(() => { |
| 44 | + appendFileStub = sinon.stub(fs, 'appendFile').yields(null); |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + appendFileStub.restore(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should append script to file', () => { |
| 52 | + const completion = new Completion('zsh'); |
| 53 | + return completion.appendScript('test script') |
| 54 | + .then(() => { |
| 55 | + appendFileStub.calledOnce.should.be.true(); |
| 56 | + appendFileStub.firstCall.args[0].should.equal(zshrcPath); |
| 57 | + appendFileStub.firstCall.args[1].should.equal('\ntest script\n'); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should reject with CompletionScriptError on fs error', () => { |
| 62 | + const completion = new Completion('zsh'); |
| 63 | + appendFileStub.yields(new Error('File write error')); |
| 64 | + return completion.appendScript('test script') |
| 65 | + .should.be.rejectedWith(CompletionScriptError); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('getScript()', () => { |
| 70 | + it('should return zsh script for zsh shell', () => { |
| 71 | + const completion = new Completion('zsh'); |
| 72 | + return completion.getScript() |
| 73 | + .then((script) => { |
| 74 | + script.should.containEql('# tldr zsh completion'); |
| 75 | + script.should.containEql('fpath=('); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return bash script for bash shell', () => { |
| 80 | + const completion = new Completion('bash'); |
| 81 | + const readFileStub = sinon.stub(fs, 'readFile').yields(null, '# bash completion script'); |
| 82 | + |
| 83 | + return completion.getScript() |
| 84 | + .then((script) => { |
| 85 | + script.should.equal('# bash completion script'); |
| 86 | + readFileStub.restore(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('getZshScript()', () => { |
| 92 | + it('should return zsh completion script', () => { |
| 93 | + const completion = new Completion('zsh'); |
| 94 | + const script = completion.getZshScript(); |
| 95 | + script.should.containEql('# tldr zsh completion'); |
| 96 | + script.should.containEql('fpath=('); |
| 97 | + script.should.containEql('compinit'); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('getBashScript()', () => { |
| 102 | + let readFileStub; |
| 103 | + |
| 104 | + beforeEach(() => { |
| 105 | + readFileStub = sinon.stub(fs, 'readFile'); |
| 106 | + }); |
| 107 | + |
| 108 | + afterEach(() => { |
| 109 | + readFileStub.restore(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should return bash completion script', () => { |
| 113 | + const completion = new Completion('bash'); |
| 114 | + readFileStub.yields(null, '# bash completion script'); |
| 115 | + |
| 116 | + return completion.getBashScript() |
| 117 | + .then((script) => { |
| 118 | + script.should.equal('# bash completion script'); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should reject with CompletionScriptError on fs error', () => { |
| 123 | + const completion = new Completion('bash'); |
| 124 | + readFileStub.yields(new Error('File read error')); |
| 125 | + |
| 126 | + return completion.getBashScript() |
| 127 | + .should.be.rejectedWith(CompletionScriptError); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments