|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | +import * as sinon from 'sinon'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as util from '../../util'; |
| 6 | +import { mockExtensionContext } from '../common/mockvscode'; |
| 7 | + |
| 8 | +const extension_root: string = path.join(__dirname, '..', '..', '..'); |
| 9 | + |
| 10 | +suite('Sess Install Test Suite', () => { |
| 11 | + let sandbox: sinon.SinonSandbox; |
| 12 | + let originalSessionWatcher: boolean | undefined; |
| 13 | + |
| 14 | + setup(() => { |
| 15 | + sandbox = sinon.createSandbox(); |
| 16 | + mockExtensionContext(extension_root, sandbox); |
| 17 | + originalSessionWatcher = vscode.workspace.getConfiguration('r').get<boolean>('sessionWatcher'); |
| 18 | + }); |
| 19 | + |
| 20 | + teardown(async () => { |
| 21 | + await vscode.workspace.getConfiguration('r').update('sessionWatcher', originalSessionWatcher, vscode.ConfigurationTarget.Global); |
| 22 | + sandbox.restore(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('promptToInstallSessPackage does nothing if sessionWatcher is disabled', async () => { |
| 26 | + await vscode.workspace.getConfiguration('r').update('sessionWatcher', false, vscode.ConfigurationTarget.Global); |
| 27 | + |
| 28 | + const getVersionStub = sandbox.stub(util, 'getRPackageVersion').resolves(undefined); |
| 29 | + const showMessageStub = sandbox.stub(vscode.window, 'showErrorMessage').resolves(undefined); |
| 30 | + |
| 31 | + await util.promptToInstallSessPackage(); |
| 32 | + |
| 33 | + assert.strictEqual(getVersionStub.called, false); |
| 34 | + assert.strictEqual(showMessageStub.called, false); |
| 35 | + }); |
| 36 | + |
| 37 | + test('promptToInstallSessPackage prompts to install if not installed', async () => { |
| 38 | + await vscode.workspace.getConfiguration('r').update('sessionWatcher', true, vscode.ConfigurationTarget.Global); |
| 39 | + sandbox.stub(util, 'getRPackageVersion').resolves(undefined); |
| 40 | + |
| 41 | + // Mock reading DESCRIPTION file |
| 42 | + sandbox.stub(util, 'readFileSyncSafe').returns('Package: sess\nVersion: 0.1.0\n'); |
| 43 | + |
| 44 | + const showMessageStub = sandbox.stub(vscode.window, 'showErrorMessage').resolves(undefined); |
| 45 | + |
| 46 | + await util.promptToInstallSessPackage(); |
| 47 | + |
| 48 | + assert.strictEqual(showMessageStub.calledOnce, true); |
| 49 | + const args = showMessageStub.getCall(0).args; |
| 50 | + assert.ok(args[0].includes('required for the session watcher to work')); |
| 51 | + }); |
| 52 | + |
| 53 | + test('promptToInstallSessPackage prompts to update if installed version is older', async () => { |
| 54 | + await vscode.workspace.getConfiguration('r').update('sessionWatcher', true, vscode.ConfigurationTarget.Global); |
| 55 | + sandbox.stub(util, 'getRPackageVersion').resolves('0.0.9'); |
| 56 | + |
| 57 | + // Mock reading DESCRIPTION file with newer version |
| 58 | + sandbox.stub(util, 'readFileSyncSafe').returns('Package: sess\nVersion: 0.1.0\n'); |
| 59 | + |
| 60 | + const showMessageStub = sandbox.stub(vscode.window, 'showErrorMessage').resolves(undefined); |
| 61 | + |
| 62 | + await util.promptToInstallSessPackage(); |
| 63 | + |
| 64 | + assert.strictEqual(showMessageStub.calledOnce, true); |
| 65 | + const args = showMessageStub.getCall(0).args; |
| 66 | + assert.ok(args[0].includes('A newer version of R package "sess" (0.1.0) is available')); |
| 67 | + }); |
| 68 | + |
| 69 | + test('promptToInstallSessPackage does not prompt if installed version is newer or equal', async () => { |
| 70 | + await vscode.workspace.getConfiguration('r').update('sessionWatcher', true, vscode.ConfigurationTarget.Global); |
| 71 | + sandbox.stub(util, 'getRPackageVersion').resolves('0.1.0'); |
| 72 | + |
| 73 | + sandbox.stub(util, 'readFileSyncSafe').returns('Package: sess\nVersion: 0.1.0\n'); |
| 74 | + |
| 75 | + const showMessageStub = sandbox.stub(vscode.window, 'showErrorMessage').resolves(undefined); |
| 76 | + |
| 77 | + await util.promptToInstallSessPackage(); |
| 78 | + |
| 79 | + assert.strictEqual(showMessageStub.called, false); |
| 80 | + }); |
| 81 | +}); |
0 commit comments