|
| 1 | +// |
| 2 | +// Note: This example test is leveraging the Mocha test framework. |
| 3 | +// Please refer to their documentation on https://mochajs.org/ for help. |
| 4 | +// |
| 5 | + |
| 6 | +// The module 'assert' provides assertion methods from node |
| 7 | +import * as assert from "assert"; |
| 8 | + |
| 9 | +// You can import and use all API from the 'vscode' module |
| 10 | +// as well as import your extension to test it |
| 11 | +import * as fs from "fs"; |
| 12 | +import * as path from "path"; |
| 13 | +import * as vscode from "vscode"; |
| 14 | +import * as testUtil from "./testUtil"; |
| 15 | +import { Uri } from "vscode"; |
| 16 | +import { Svn } from "../svn"; |
| 17 | +import { Model } from "../model"; |
| 18 | +import { SvnFinder, ISvn } from "../svnFinder"; |
| 19 | +import { Repository } from "../repository"; |
| 20 | + |
| 21 | +// Defines a Mocha test suite to group tests of similar kind together |
| 22 | +suite("Repository Tests", () => { |
| 23 | + let repoUri: Uri; |
| 24 | + let checkoutDir: Uri; |
| 25 | + let svnFinder: SvnFinder; |
| 26 | + let info: ISvn; |
| 27 | + let svn: Svn; |
| 28 | + let model: Model; |
| 29 | + |
| 30 | + suiteSetup(async () => { |
| 31 | + repoUri = await testUtil.createRepoServer(); |
| 32 | + await testUtil.createStandardLayout(testUtil.getSvnUrl(repoUri)); |
| 33 | + checkoutDir = await testUtil.createRepoCheckout( |
| 34 | + testUtil.getSvnUrl(repoUri) + "/trunk" |
| 35 | + ); |
| 36 | + |
| 37 | + svnFinder = new SvnFinder(); |
| 38 | + info = await svnFinder.findSvn(); |
| 39 | + svn = new Svn({ svnPath: info.path, version: info.version }); |
| 40 | + model = new Model(svn); |
| 41 | + await model.tryOpenRepository(checkoutDir.fsPath); |
| 42 | + }); |
| 43 | + |
| 44 | + suiteTeardown(() => { |
| 45 | + testUtil.destroyAllTempPaths(); |
| 46 | + }); |
| 47 | + |
| 48 | + test("Find Repository", async () => { |
| 49 | + assert.ok(info); |
| 50 | + assert.ok(info.path); |
| 51 | + assert.ok(info.version); |
| 52 | + }); |
| 53 | + |
| 54 | + test("Try Open Repository", async function() { |
| 55 | + assert.equal(model.repositories.length, 1); |
| 56 | + }); |
| 57 | + |
| 58 | + test("Try Open Repository Again", async () => { |
| 59 | + await model.tryOpenRepository(checkoutDir.fsPath); |
| 60 | + assert.equal(model.repositories.length, 1); |
| 61 | + }); |
| 62 | + |
| 63 | + test("Try get repository from Uri", () => { |
| 64 | + const repository = model.getRepository(checkoutDir); |
| 65 | + assert.ok(repository); |
| 66 | + }); |
| 67 | + |
| 68 | + test("Try get repository from string", () => { |
| 69 | + const repository = model.getRepository(checkoutDir.fsPath); |
| 70 | + assert.ok(repository); |
| 71 | + }); |
| 72 | + |
| 73 | + test("Try get repository from repository", () => { |
| 74 | + const repository = model.getRepository(checkoutDir.fsPath); |
| 75 | + const repository2 = model.getRepository(repository); |
| 76 | + assert.ok(repository2); |
| 77 | + assert.equal(repository, repository2); |
| 78 | + }); |
| 79 | + |
| 80 | + test("Try get current branch name", async () => { |
| 81 | + const repository: Repository | undefined = model.getRepository( |
| 82 | + checkoutDir.fsPath |
| 83 | + ); |
| 84 | + if (!repository) return; |
| 85 | + |
| 86 | + const name = await repository.getCurrentBranch(); |
| 87 | + assert.equal(name, "trunk"); |
| 88 | + }); |
| 89 | + |
| 90 | + test("Try commit file", async function() { |
| 91 | + this.timeout(60000); |
| 92 | + const repository: Repository | undefined = model.getRepository( |
| 93 | + checkoutDir.fsPath |
| 94 | + ); |
| 95 | + if (!repository) return; |
| 96 | + |
| 97 | + assert.equal(repository.changes.resourceStates.length, 0); |
| 98 | + |
| 99 | + const file = path.join(checkoutDir.fsPath, "new.txt"); |
| 100 | + |
| 101 | + await repository.update(); |
| 102 | + fs.writeFileSync(file, "test"); |
| 103 | + |
| 104 | + await repository.addFile(file); |
| 105 | + |
| 106 | + await repository.update(); |
| 107 | + await testUtil.delay(1500); //Wait the debounce time |
| 108 | + assert.equal(repository.changes.resourceStates.length, 1); |
| 109 | + |
| 110 | + const message = await repository.repository.commitFiles("First Commit", [ |
| 111 | + file |
| 112 | + ]); |
| 113 | + assert.ok(/Committed revision (.*)\./i.test(message)); |
| 114 | + |
| 115 | + await repository.update(); |
| 116 | + await testUtil.delay(1500); //Wait the debounce time |
| 117 | + assert.equal(repository.changes.resourceStates.length, 0); |
| 118 | + |
| 119 | + const remoteContent = await repository.show(file, "HEAD"); |
| 120 | + assert.equal(remoteContent, "test"); |
| 121 | + }); |
| 122 | + |
| 123 | + test("Try switch branch", async function() { |
| 124 | + this.timeout(60000); |
| 125 | + const newCheckoutDir = await testUtil.createRepoCheckout( |
| 126 | + testUtil.getSvnUrl(repoUri) + "/trunk" |
| 127 | + ); |
| 128 | + |
| 129 | + await model.tryOpenRepository(newCheckoutDir.fsPath); |
| 130 | + |
| 131 | + const newRepository: Repository | undefined = model.getRepository( |
| 132 | + newCheckoutDir.fsPath |
| 133 | + ); |
| 134 | + if (!newRepository) return; |
| 135 | + assert.ok(newRepository); |
| 136 | + |
| 137 | + const isSwitched = await newRepository.branch("test"); |
| 138 | + assert.ok(isSwitched); |
| 139 | + |
| 140 | + const currentBranch = await newRepository.getCurrentBranch(); |
| 141 | + |
| 142 | + assert.equal(currentBranch, "test"); |
| 143 | + }); |
| 144 | +}); |
0 commit comments