| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {AbsolutePath, RunnableOperation, Submodule} from 'isl/src/types'; |
| b69ab31 | | | 9 | import type {ResolveCommandConflictOutput} from '../commands'; |
| b69ab31 | | | 10 | import type {ServerPlatform} from '../serverPlatform'; |
| b69ab31 | | | 11 | import type {RepositoryContext} from '../serverTypes'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import {CommandRunner, type MergeConflicts, type ValidatedRepoInfo} from 'isl/src/types'; |
| b69ab31 | | | 14 | import fs from 'node:fs'; |
| b69ab31 | | | 15 | import os from 'node:os'; |
| b69ab31 | | | 16 | import path from 'node:path'; |
| b69ab31 | | | 17 | import * as ejeca from 'shared/ejeca'; |
| b69ab31 | | | 18 | import * as fsUtils from 'shared/fs'; |
| b69ab31 | | | 19 | import {clone, mockLogger, nextTick} from 'shared/testUtils'; |
| b69ab31 | | | 20 | import {absolutePathForFileInRepo, Repository} from '../Repository'; |
| b69ab31 | | | 21 | import {makeServerSideTracker} from '../analytics/serverSideTracker'; |
| b69ab31 | | | 22 | import {extractRepoInfoFromUrl, setConfigOverrideForTests} from '../commands'; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | /* eslint-disable require-await */ |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | jest.mock('../WatchForChanges', () => { |
| b69ab31 | | | 27 | class MockWatchForChanges { |
| b69ab31 | | | 28 | dispose = jest.fn(); |
| b69ab31 | | | 29 | poll = jest.fn(); |
| b69ab31 | | | 30 | } |
| b69ab31 | | | 31 | return {WatchForChanges: MockWatchForChanges}; |
| b69ab31 | | | 32 | }); |
| b69ab31 | | | 33 | |
| b69ab31 | | | 34 | const mockTracker = makeServerSideTracker( |
| b69ab31 | | | 35 | mockLogger, |
| b69ab31 | | | 36 | {platformName: 'test'} as ServerPlatform, |
| b69ab31 | | | 37 | '0.1', |
| b69ab31 | | | 38 | jest.fn(), |
| b69ab31 | | | 39 | ); |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | function mockEjeca( |
| b69ab31 | | | 42 | cmds: Array<[RegExp, (() => {stdout: string} | Error) | {stdout: string} | Error]>, |
| b69ab31 | | | 43 | ) { |
| b69ab31 | | | 44 | return jest.spyOn(ejeca, 'ejeca').mockImplementation(((cmd: string, args: Array<string>) => { |
| b69ab31 | | | 45 | const argStr = cmd + ' ' + args?.join(' '); |
| b69ab31 | | | 46 | const ejecaOther = { |
| b69ab31 | | | 47 | kill: jest.fn(), |
| b69ab31 | | | 48 | on: jest.fn((event, cb) => { |
| b69ab31 | | | 49 | // immediately call exit cb to teardown timeout |
| b69ab31 | | | 50 | if (event === 'exit') { |
| b69ab31 | | | 51 | cb(); |
| b69ab31 | | | 52 | } |
| b69ab31 | | | 53 | }), |
| b69ab31 | | | 54 | }; |
| b69ab31 | | | 55 | for (const [regex, output] of cmds) { |
| b69ab31 | | | 56 | if (regex.test(argStr)) { |
| b69ab31 | | | 57 | let value = output; |
| b69ab31 | | | 58 | if (typeof output === 'function') { |
| b69ab31 | | | 59 | value = output(); |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | if (value instanceof Error) { |
| b69ab31 | | | 62 | throw value; |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | return {...ejecaOther, ...value}; |
| b69ab31 | | | 65 | } |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | return {...ejecaOther, stdout: ''}; |
| b69ab31 | | | 68 | }) as unknown as typeof ejeca.ejeca); |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | function processExitError(code: number, message: string): ejeca.EjecaError { |
| b69ab31 | | | 72 | const err = new Error(message) as ejeca.EjecaError; |
| b69ab31 | | | 73 | err.exitCode = code; |
| b69ab31 | | | 74 | return err; |
| b69ab31 | | | 75 | } |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | function setPathsDefault(path: string) { |
| b69ab31 | | | 78 | setConfigOverrideForTests([['paths.default', path]], false); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | describe('Repository', () => { |
| b69ab31 | | | 82 | let ctx: RepositoryContext; |
| b69ab31 | | | 83 | beforeEach(() => { |
| b69ab31 | | | 84 | ctx = { |
| b69ab31 | | | 85 | cmd: 'sl', |
| b69ab31 | | | 86 | cwd: '/path/to/cwd', |
| b69ab31 | | | 87 | logger: mockLogger, |
| b69ab31 | | | 88 | tracker: mockTracker, |
| b69ab31 | | | 89 | }; |
| b69ab31 | | | 90 | }); |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | it('setting command name', async () => { |
| b69ab31 | | | 93 | const ejecaSpy = mockEjeca([]); |
| b69ab31 | | | 94 | await Repository.getRepoInfo({...ctx, cmd: 'slb'}); |
| b69ab31 | | | 95 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 96 | 'slb', |
| b69ab31 | | | 97 | expect.arrayContaining(['root']), |
| b69ab31 | | | 98 | expect.anything(), |
| b69ab31 | | | 99 | ); |
| b69ab31 | | | 100 | }); |
| b69ab31 | | | 101 | |
| b69ab31 | | | 102 | describe('extracting github repo info', () => { |
| b69ab31 | | | 103 | beforeEach(() => { |
| b69ab31 | | | 104 | setConfigOverrideForTests([['github.pull_request_domain', 'github.com']]); |
| b69ab31 | | | 105 | mockEjeca([ |
| b69ab31 | | | 106 | [/^sl root --dotdir/, {stdout: '/path/to/myRepo/.sl'}], |
| b69ab31 | | | 107 | [/^sl root/, {stdout: '/path/to/myRepo'}], |
| b69ab31 | | | 108 | [/^sl debugroots/, {stdout: '/path/to/myRepo'}], |
| b69ab31 | | | 109 | [ |
| b69ab31 | | | 110 | /^gh auth status --hostname gitlab.myCompany.com/, |
| b69ab31 | | | 111 | new Error('not authenticated on this hostname'), |
| b69ab31 | | | 112 | ], |
| b69ab31 | | | 113 | [/^gh auth status --hostname ghe.myCompany.com/, {stdout: ''}], |
| b69ab31 | | | 114 | ]); |
| b69ab31 | | | 115 | }); |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | it('extracting github repo info', async () => { |
| b69ab31 | | | 118 | setPathsDefault('https://github.com/myUsername/myRepo.git'); |
| b69ab31 | | | 119 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 120 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 121 | expect(repo.info).toEqual({ |
| b69ab31 | | | 122 | type: 'success', |
| b69ab31 | | | 123 | command: 'sl', |
| b69ab31 | | | 124 | repoRoot: '/path/to/myRepo', |
| b69ab31 | | | 125 | repoRoots: ['/path/to/myRepo'], |
| b69ab31 | | | 126 | dotdir: '/path/to/myRepo/.sl', |
| b69ab31 | | | 127 | codeReviewSystem: { |
| b69ab31 | | | 128 | type: 'github', |
| b69ab31 | | | 129 | owner: 'myUsername', |
| b69ab31 | | | 130 | repo: 'myRepo', |
| b69ab31 | | | 131 | hostname: 'github.com', |
| b69ab31 | | | 132 | }, |
| b69ab31 | | | 133 | pullRequestDomain: 'github.com', |
| b69ab31 | | | 134 | isEdenFs: false, |
| b69ab31 | | | 135 | }); |
| b69ab31 | | | 136 | }); |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | it('extracting github enterprise repo info', async () => { |
| b69ab31 | | | 139 | setPathsDefault('https://ghe.myCompany.com/myUsername/myRepo.git'); |
| b69ab31 | | | 140 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 141 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 142 | expect(repo.info).toEqual({ |
| b69ab31 | | | 143 | type: 'success', |
| b69ab31 | | | 144 | command: 'sl', |
| b69ab31 | | | 145 | repoRoot: '/path/to/myRepo', |
| b69ab31 | | | 146 | repoRoots: ['/path/to/myRepo'], |
| b69ab31 | | | 147 | dotdir: '/path/to/myRepo/.sl', |
| b69ab31 | | | 148 | codeReviewSystem: { |
| b69ab31 | | | 149 | type: 'github', |
| b69ab31 | | | 150 | owner: 'myUsername', |
| b69ab31 | | | 151 | repo: 'myRepo', |
| b69ab31 | | | 152 | hostname: 'ghe.myCompany.com', |
| b69ab31 | | | 153 | }, |
| b69ab31 | | | 154 | pullRequestDomain: 'github.com', |
| b69ab31 | | | 155 | isEdenFs: false, |
| b69ab31 | | | 156 | }); |
| b69ab31 | | | 157 | }); |
| b69ab31 | | | 158 | |
| b69ab31 | | | 159 | it('handles non-github-enterprise unknown code review providers', async () => { |
| b69ab31 | | | 160 | setPathsDefault('https://gitlab.myCompany.com/myUsername/myRepo.git'); |
| b69ab31 | | | 161 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 162 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 163 | expect(repo.info).toEqual({ |
| b69ab31 | | | 164 | type: 'success', |
| b69ab31 | | | 165 | command: 'sl', |
| b69ab31 | | | 166 | repoRoot: '/path/to/myRepo', |
| b69ab31 | | | 167 | repoRoots: ['/path/to/myRepo'], |
| b69ab31 | | | 168 | dotdir: '/path/to/myRepo/.sl', |
| b69ab31 | | | 169 | codeReviewSystem: { |
| b69ab31 | | | 170 | type: 'unknown', |
| b69ab31 | | | 171 | path: 'https://gitlab.myCompany.com/myUsername/myRepo.git', |
| b69ab31 | | | 172 | }, |
| b69ab31 | | | 173 | pullRequestDomain: 'github.com', |
| b69ab31 | | | 174 | isEdenFs: false, |
| b69ab31 | | | 175 | }); |
| b69ab31 | | | 176 | }); |
| b69ab31 | | | 177 | }); |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | it('applies isl.hold-off-refresh-ms config', async () => { |
| b69ab31 | | | 180 | setConfigOverrideForTests([['isl.hold-off-refresh-ms', '12345']], false); |
| b69ab31 | | | 181 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 182 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 183 | await new Promise(process.nextTick); |
| b69ab31 | | | 184 | expect(repo.configHoldOffRefreshMs).toBe(12345); |
| b69ab31 | | | 185 | }); |
| b69ab31 | | | 186 | |
| b69ab31 | | | 187 | it('extracting repo info', async () => { |
| b69ab31 | | | 188 | setConfigOverrideForTests([]); |
| b69ab31 | | | 189 | setPathsDefault('mononoke://0.0.0.0/fbsource'); |
| b69ab31 | | | 190 | mockEjeca([ |
| b69ab31 | | | 191 | [/^sl root --dotdir/, {stdout: '/path/to/myRepo/.sl'}], |
| b69ab31 | | | 192 | [/^sl root/, {stdout: '/path/to/myRepo'}], |
| b69ab31 | | | 193 | [/^sl debugroots/, {stdout: '/path/to/myRepo/submodule\n/path/to/myRepo'}], |
| b69ab31 | | | 194 | ]); |
| b69ab31 | | | 195 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 196 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 197 | expect(repo.info).toEqual({ |
| b69ab31 | | | 198 | type: 'success', |
| b69ab31 | | | 199 | command: 'sl', |
| b69ab31 | | | 200 | repoRoot: '/path/to/myRepo', |
| b69ab31 | | | 201 | repoRoots: ['/path/to/myRepo', '/path/to/myRepo/submodule'], |
| b69ab31 | | | 202 | dotdir: '/path/to/myRepo/.sl', |
| b69ab31 | | | 203 | codeReviewSystem: expect.anything(), |
| b69ab31 | | | 204 | pullRequestDomain: undefined, |
| b69ab31 | | | 205 | isEdenFs: false, |
| b69ab31 | | | 206 | }); |
| b69ab31 | | | 207 | }); |
| b69ab31 | | | 208 | |
| b69ab31 | | | 209 | it('handles cwd not exists', async () => { |
| b69ab31 | | | 210 | const err = new Error('cwd does not exist') as Error & {code: string}; |
| b69ab31 | | | 211 | err.code = 'ENOENT'; |
| b69ab31 | | | 212 | mockEjeca([[/^sl root/, err]]); |
| b69ab31 | | | 213 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 214 | expect(info).toEqual({ |
| b69ab31 | | | 215 | type: 'cwdDoesNotExist', |
| b69ab31 | | | 216 | cwd: '/path/to/cwd', |
| b69ab31 | | | 217 | }); |
| b69ab31 | | | 218 | }); |
| b69ab31 | | | 219 | |
| b69ab31 | | | 220 | it('handles missing executables on windows', async () => { |
| b69ab31 | | | 221 | const osSpy = jest.spyOn(os, 'platform').mockImplementation(() => 'win32'); |
| b69ab31 | | | 222 | mockEjeca([ |
| b69ab31 | | | 223 | [ |
| b69ab31 | | | 224 | /^sl root/, |
| b69ab31 | | | 225 | processExitError( |
| b69ab31 | | | 226 | /* code */ 1, |
| b69ab31 | | | 227 | `'sl' is not recognized as an internal or external command, operable program or batch file.`, |
| b69ab31 | | | 228 | ), |
| b69ab31 | | | 229 | ], |
| b69ab31 | | | 230 | ]); |
| b69ab31 | | | 231 | jest.spyOn(fsUtils, 'exists').mockImplementation(async () => true); |
| b69ab31 | | | 232 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 233 | expect(info).toEqual({ |
| b69ab31 | | | 234 | type: 'invalidCommand', |
| b69ab31 | | | 235 | command: 'sl', |
| b69ab31 | | | 236 | path: expect.anything(), |
| b69ab31 | | | 237 | }); |
| b69ab31 | | | 238 | osSpy.mockRestore(); |
| b69ab31 | | | 239 | }); |
| b69ab31 | | | 240 | |
| b69ab31 | | | 241 | it('prevents setting configs not in the allowlist', async () => { |
| b69ab31 | | | 242 | setConfigOverrideForTests([]); |
| b69ab31 | | | 243 | setPathsDefault('mononoke://0.0.0.0/fbsource'); |
| b69ab31 | | | 244 | mockEjeca([ |
| b69ab31 | | | 245 | [/^sl root --dotdir/, {stdout: '/path/to/myRepo/.sl'}], |
| b69ab31 | | | 246 | [/^sl root/, {stdout: '/path/to/myRepo'}], |
| b69ab31 | | | 247 | ]); |
| b69ab31 | | | 248 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 249 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 250 | // @ts-expect-error We expect a type error in addition to runtime validation |
| b69ab31 | | | 251 | await expect(repo.setConfig(ctx, 'user', 'some-random-config', 'hi')).rejects.toEqual( |
| b69ab31 | | | 252 | new Error('config some-random-config not in allowlist for settable configs'), |
| b69ab31 | | | 253 | ); |
| b69ab31 | | | 254 | }); |
| b69ab31 | | | 255 | |
| b69ab31 | | | 256 | describe('running operations', () => { |
| b69ab31 | | | 257 | const repoInfo: ValidatedRepoInfo = { |
| b69ab31 | | | 258 | type: 'success', |
| b69ab31 | | | 259 | command: 'sl', |
| b69ab31 | | | 260 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 261 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 262 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 263 | pullRequestDomain: undefined, |
| b69ab31 | | | 264 | isEdenFs: false, |
| b69ab31 | | | 265 | }; |
| b69ab31 | | | 266 | |
| b69ab31 | | | 267 | let ejecaSpy: ReturnType<typeof mockEjeca>; |
| b69ab31 | | | 268 | beforeEach(() => { |
| b69ab31 | | | 269 | ejecaSpy = mockEjeca([]); |
| b69ab31 | | | 270 | }); |
| b69ab31 | | | 271 | |
| b69ab31 | | | 272 | async function runOperation(op: Partial<RunnableOperation>) { |
| b69ab31 | | | 273 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 274 | const progressSpy = jest.fn(); |
| b69ab31 | | | 275 | |
| b69ab31 | | | 276 | await repo.runOrQueueOperation( |
| b69ab31 | | | 277 | ctx, |
| b69ab31 | | | 278 | { |
| b69ab31 | | | 279 | id: '1', |
| b69ab31 | | | 280 | trackEventName: 'CommitOperation', |
| b69ab31 | | | 281 | args: [], |
| b69ab31 | | | 282 | runner: CommandRunner.Sapling, |
| b69ab31 | | | 283 | ...op, |
| b69ab31 | | | 284 | }, |
| b69ab31 | | | 285 | progressSpy, |
| b69ab31 | | | 286 | ); |
| b69ab31 | | | 287 | } |
| b69ab31 | | | 288 | |
| b69ab31 | | | 289 | it('runs operations', async () => { |
| b69ab31 | | | 290 | await runOperation({ |
| b69ab31 | | | 291 | args: ['commit', '--message', 'hi'], |
| b69ab31 | | | 292 | }); |
| b69ab31 | | | 293 | |
| b69ab31 | | | 294 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 295 | 'sl', |
| b69ab31 | | | 296 | ['commit', '--message', 'hi', '--noninteractive'], |
| b69ab31 | | | 297 | expect.anything(), |
| b69ab31 | | | 298 | ); |
| b69ab31 | | | 299 | }); |
| b69ab31 | | | 300 | |
| b69ab31 | | | 301 | it('handles succeedable revsets', async () => { |
| b69ab31 | | | 302 | await runOperation({ |
| b69ab31 | | | 303 | args: ['rebase', '--rev', {type: 'succeedable-revset', revset: 'aaa'}], |
| b69ab31 | | | 304 | }); |
| b69ab31 | | | 305 | |
| b69ab31 | | | 306 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 307 | 'sl', |
| b69ab31 | | | 308 | ['rebase', '--rev', 'max(successors(aaa))', '--noninteractive'], |
| b69ab31 | | | 309 | expect.anything(), |
| b69ab31 | | | 310 | ); |
| b69ab31 | | | 311 | }); |
| b69ab31 | | | 312 | |
| b69ab31 | | | 313 | it('handles exact revsets', async () => { |
| b69ab31 | | | 314 | await runOperation({ |
| b69ab31 | | | 315 | args: ['rebase', '--rev', {type: 'exact-revset', revset: 'aaa'}], |
| b69ab31 | | | 316 | }); |
| b69ab31 | | | 317 | |
| b69ab31 | | | 318 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 319 | 'sl', |
| b69ab31 | | | 320 | ['rebase', '--rev', 'aaa', '--noninteractive'], |
| b69ab31 | | | 321 | expect.anything(), |
| b69ab31 | | | 322 | ); |
| b69ab31 | | | 323 | }); |
| b69ab31 | | | 324 | |
| b69ab31 | | | 325 | it('handles repo-relative files', async () => { |
| b69ab31 | | | 326 | await runOperation({ |
| b69ab31 | | | 327 | args: ['add', {type: 'repo-relative-file', path: 'path/to/file.txt'}], |
| b69ab31 | | | 328 | }); |
| b69ab31 | | | 329 | |
| b69ab31 | | | 330 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 331 | 'sl', |
| b69ab31 | | | 332 | ['add', '../repo/path/to/file.txt', '--noninteractive'], |
| b69ab31 | | | 333 | expect.anything(), |
| b69ab31 | | | 334 | ); |
| b69ab31 | | | 335 | }); |
| b69ab31 | | | 336 | |
| b69ab31 | | | 337 | it('handles allowed configs', async () => { |
| b69ab31 | | | 338 | await runOperation({ |
| b69ab31 | | | 339 | args: ['commit', {type: 'config', key: 'ui.allowemptycommit', value: 'True'}], |
| b69ab31 | | | 340 | }); |
| b69ab31 | | | 341 | |
| b69ab31 | | | 342 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 343 | 'sl', |
| b69ab31 | | | 344 | ['commit', '--config', 'ui.allowemptycommit=True', '--noninteractive'], |
| b69ab31 | | | 345 | expect.anything(), |
| b69ab31 | | | 346 | ); |
| b69ab31 | | | 347 | }); |
| b69ab31 | | | 348 | |
| b69ab31 | | | 349 | it('disallows some commands', async () => { |
| b69ab31 | | | 350 | await runOperation({ |
| b69ab31 | | | 351 | args: ['debugsh'], |
| b69ab31 | | | 352 | }); |
| b69ab31 | | | 353 | |
| b69ab31 | | | 354 | expect(ejecaSpy).not.toHaveBeenCalledWith( |
| b69ab31 | | | 355 | 'sl', |
| b69ab31 | | | 356 | ['debugsh', '--noninteractive'], |
| b69ab31 | | | 357 | expect.anything(), |
| b69ab31 | | | 358 | ); |
| b69ab31 | | | 359 | }); |
| b69ab31 | | | 360 | |
| b69ab31 | | | 361 | it('disallows unknown configs', async () => { |
| b69ab31 | | | 362 | await runOperation({ |
| b69ab31 | | | 363 | args: ['commit', {type: 'config', key: 'foo.bar', value: '1'}], |
| b69ab31 | | | 364 | }); |
| b69ab31 | | | 365 | |
| b69ab31 | | | 366 | expect(ejecaSpy).not.toHaveBeenCalledWith( |
| b69ab31 | | | 367 | 'sl', |
| b69ab31 | | | 368 | expect.arrayContaining(['commit', '--config', 'foo.bar=1']), |
| b69ab31 | | | 369 | expect.anything(), |
| b69ab31 | | | 370 | ); |
| b69ab31 | | | 371 | }); |
| b69ab31 | | | 372 | |
| b69ab31 | | | 373 | it('disallows unstructured --config flag', async () => { |
| b69ab31 | | | 374 | await runOperation({ |
| b69ab31 | | | 375 | args: ['commit', '--config', 'foo.bar=1'], |
| b69ab31 | | | 376 | }); |
| b69ab31 | | | 377 | |
| b69ab31 | | | 378 | expect(ejecaSpy).not.toHaveBeenCalledWith( |
| b69ab31 | | | 379 | 'sl', |
| b69ab31 | | | 380 | expect.arrayContaining(['commit', '--config', 'foo.bar=1']), |
| b69ab31 | | | 381 | expect.anything(), |
| b69ab31 | | | 382 | ); |
| b69ab31 | | | 383 | }); |
| b69ab31 | | | 384 | }); |
| b69ab31 | | | 385 | |
| b69ab31 | | | 386 | describe('fetchSloc', () => { |
| b69ab31 | | | 387 | const repoInfo: ValidatedRepoInfo = { |
| b69ab31 | | | 388 | type: 'success', |
| b69ab31 | | | 389 | command: 'sl', |
| b69ab31 | | | 390 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 391 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 392 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 393 | pullRequestDomain: undefined, |
| b69ab31 | | | 394 | isEdenFs: false, |
| b69ab31 | | | 395 | }; |
| b69ab31 | | | 396 | |
| b69ab31 | | | 397 | const EXAMPLE_DIFFSTAT = ` |
| b69ab31 | | | 398 | | 34 ++++++++++ |
| b69ab31 | | | 399 | www/flib/intern/entity/diff/EntPhabricatorDiffSchema.php | 11 +++ |
| b69ab31 | | | 400 | 2 files changed, 45 insertions(+), 0 deletions(-)\n`; |
| b69ab31 | | | 401 | |
| b69ab31 | | | 402 | it('parses sloc', async () => { |
| b69ab31 | | | 403 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 404 | |
| b69ab31 | | | 405 | const ejecaSpy = mockEjeca([[/^sl diff/, () => ({stdout: EXAMPLE_DIFFSTAT})]]); |
| b69ab31 | | | 406 | const results = repo.fetchSignificantLinesOfCode(ctx, 'abcdef', ['generated.file']); |
| b69ab31 | | | 407 | await expect(results).resolves.toEqual(45); |
| b69ab31 | | | 408 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 409 | 'sl', |
| b69ab31 | | | 410 | expect.arrayContaining([ |
| b69ab31 | | | 411 | 'diff', |
| b69ab31 | | | 412 | '-B', |
| b69ab31 | | | 413 | '-X', |
| b69ab31 | | | 414 | '**__generated__**', |
| b69ab31 | | | 415 | '-X', |
| b69ab31 | | | 416 | '/path/to/repo/generated.file', |
| b69ab31 | | | 417 | '-c', |
| b69ab31 | | | 418 | 'abcdef', |
| b69ab31 | | | 419 | ]), |
| b69ab31 | | | 420 | expect.anything(), |
| b69ab31 | | | 421 | ); |
| b69ab31 | | | 422 | }); |
| b69ab31 | | | 423 | |
| b69ab31 | | | 424 | it('handles empty generated list', async () => { |
| b69ab31 | | | 425 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 426 | const ejecaSpy = mockEjeca([[/^sl diff/, () => ({stdout: EXAMPLE_DIFFSTAT})]]); |
| b69ab31 | | | 427 | repo.fetchSignificantLinesOfCode(ctx, 'abcdef', []); |
| b69ab31 | | | 428 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 429 | 'sl', |
| b69ab31 | | | 430 | expect.arrayContaining(['diff', '-B', '-X', '**__generated__**', '-c', 'abcdef']), |
| b69ab31 | | | 431 | expect.anything(), |
| b69ab31 | | | 432 | ); |
| b69ab31 | | | 433 | }); |
| b69ab31 | | | 434 | |
| b69ab31 | | | 435 | it('handles multiple generated files', async () => { |
| b69ab31 | | | 436 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 437 | const ejecaSpy = mockEjeca([[/^sl diff/, () => ({stdout: EXAMPLE_DIFFSTAT})]]); |
| b69ab31 | | | 438 | const generatedFiles = ['generated1.file', 'generated2.file']; |
| b69ab31 | | | 439 | repo.fetchSignificantLinesOfCode(ctx, 'abcdef', generatedFiles); |
| b69ab31 | | | 440 | await nextTick(); |
| b69ab31 | | | 441 | expect(ejecaSpy).toHaveBeenCalledWith( |
| b69ab31 | | | 442 | 'sl', |
| b69ab31 | | | 443 | expect.arrayContaining([ |
| b69ab31 | | | 444 | 'diff', |
| b69ab31 | | | 445 | '-B', |
| b69ab31 | | | 446 | '-X', |
| b69ab31 | | | 447 | '**__generated__**', |
| b69ab31 | | | 448 | '-X', |
| b69ab31 | | | 449 | '/path/to/repo/generated1.file', |
| b69ab31 | | | 450 | '-X', |
| b69ab31 | | | 451 | '/path/to/repo/generated2.file', |
| b69ab31 | | | 452 | '-c', |
| b69ab31 | | | 453 | 'abcdef', |
| b69ab31 | | | 454 | ]), |
| b69ab31 | | | 455 | expect.anything(), |
| b69ab31 | | | 456 | ); |
| b69ab31 | | | 457 | }); |
| b69ab31 | | | 458 | }); |
| b69ab31 | | | 459 | |
| b69ab31 | | | 460 | describe('fetchSmartlogCommits', () => { |
| b69ab31 | | | 461 | const repoInfo: ValidatedRepoInfo = { |
| b69ab31 | | | 462 | type: 'success', |
| b69ab31 | | | 463 | command: 'sl', |
| b69ab31 | | | 464 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 465 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 466 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 467 | pullRequestDomain: undefined, |
| b69ab31 | | | 468 | isEdenFs: false, |
| b69ab31 | | | 469 | }; |
| b69ab31 | | | 470 | |
| b69ab31 | | | 471 | const expectCalledWithRevset = (spy: jest.SpyInstance<unknown>, revset: string) => { |
| b69ab31 | | | 472 | expect(spy).toHaveBeenCalledWith( |
| b69ab31 | | | 473 | 'sl', |
| b69ab31 | | | 474 | expect.arrayContaining(['log', '--rev', revset]), |
| b69ab31 | | | 475 | expect.anything(), |
| b69ab31 | | | 476 | ); |
| b69ab31 | | | 477 | }; |
| b69ab31 | | | 478 | |
| b69ab31 | | | 479 | it('uses correct revset in normal case', async () => { |
| b69ab31 | | | 480 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 481 | |
| b69ab31 | | | 482 | const ejecaSpy = mockEjeca([]); |
| b69ab31 | | | 483 | |
| b69ab31 | | | 484 | await repo.fetchSmartlogCommits(); |
| b69ab31 | | | 485 | expectCalledWithRevset( |
| b69ab31 | | | 486 | ejecaSpy, |
| b69ab31 | | | 487 | 'smartlog(((interestingbookmarks() + heads(draft())) & date(-14)) + .)', |
| b69ab31 | | | 488 | ); |
| b69ab31 | | | 489 | }); |
| b69ab31 | | | 490 | |
| b69ab31 | | | 491 | it('updates revset when changing date range', async () => { |
| b69ab31 | | | 492 | const ejecaSpy = mockEjeca([]); |
| b69ab31 | | | 493 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 494 | |
| b69ab31 | | | 495 | repo.nextVisibleCommitRangeInDays(); |
| b69ab31 | | | 496 | await repo.fetchSmartlogCommits(); |
| b69ab31 | | | 497 | expectCalledWithRevset( |
| b69ab31 | | | 498 | ejecaSpy, |
| b69ab31 | | | 499 | 'smartlog(((interestingbookmarks() + heads(draft())) & date(-60)) + .)', |
| b69ab31 | | | 500 | ); |
| b69ab31 | | | 501 | |
| b69ab31 | | | 502 | repo.nextVisibleCommitRangeInDays(); |
| b69ab31 | | | 503 | await repo.fetchSmartlogCommits(); |
| b69ab31 | | | 504 | expectCalledWithRevset(ejecaSpy, 'smartlog((interestingbookmarks() + heads(draft())) + .)'); |
| b69ab31 | | | 505 | }); |
| b69ab31 | | | 506 | |
| b69ab31 | | | 507 | it('fetches additional revsets', async () => { |
| b69ab31 | | | 508 | const ejecaSpy = mockEjeca([]); |
| b69ab31 | | | 509 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 510 | |
| b69ab31 | | | 511 | repo.stableLocations = [ |
| b69ab31 | | | 512 | {name: 'mystable', hash: 'aaa', info: 'this is the stable for aaa', date: new Date(0)}, |
| b69ab31 | | | 513 | ]; |
| b69ab31 | | | 514 | await repo.fetchSmartlogCommits(); |
| b69ab31 | | | 515 | expectCalledWithRevset( |
| b69ab31 | | | 516 | ejecaSpy, |
| b69ab31 | | | 517 | 'smartlog(((interestingbookmarks() + heads(draft())) & date(-14)) + . + present(aaa))', |
| b69ab31 | | | 518 | ); |
| b69ab31 | | | 519 | |
| b69ab31 | | | 520 | repo.stableLocations = [ |
| b69ab31 | | | 521 | {name: 'mystable', hash: 'aaa', info: 'this is the stable for aaa', date: new Date(0)}, |
| b69ab31 | | | 522 | {name: '2', hash: 'bbb', info: '2', date: new Date(0)}, |
| b69ab31 | | | 523 | ]; |
| b69ab31 | | | 524 | await repo.fetchSmartlogCommits(); |
| b69ab31 | | | 525 | expectCalledWithRevset( |
| b69ab31 | | | 526 | ejecaSpy, |
| b69ab31 | | | 527 | 'smartlog(((interestingbookmarks() + heads(draft())) & date(-14)) + . + present(aaa) + present(bbb))', |
| b69ab31 | | | 528 | ); |
| b69ab31 | | | 529 | |
| b69ab31 | | | 530 | repo.nextVisibleCommitRangeInDays(); |
| b69ab31 | | | 531 | repo.nextVisibleCommitRangeInDays(); |
| b69ab31 | | | 532 | await repo.fetchSmartlogCommits(); |
| b69ab31 | | | 533 | expectCalledWithRevset( |
| b69ab31 | | | 534 | ejecaSpy, |
| b69ab31 | | | 535 | 'smartlog((interestingbookmarks() + heads(draft())) + . + present(aaa) + present(bbb))', |
| b69ab31 | | | 536 | ); |
| b69ab31 | | | 537 | }); |
| b69ab31 | | | 538 | }); |
| b69ab31 | | | 539 | |
| b69ab31 | | | 540 | describe('merge conflicts', () => { |
| b69ab31 | | | 541 | const repoInfo: ValidatedRepoInfo = { |
| b69ab31 | | | 542 | type: 'success', |
| b69ab31 | | | 543 | command: 'sl', |
| b69ab31 | | | 544 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 545 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 546 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 547 | pullRequestDomain: undefined, |
| b69ab31 | | | 548 | isEdenFs: false, |
| b69ab31 | | | 549 | }; |
| b69ab31 | | | 550 | const NOT_IN_CONFLICT: ResolveCommandConflictOutput = [ |
| b69ab31 | | | 551 | { |
| b69ab31 | | | 552 | command: null, |
| b69ab31 | | | 553 | conflicts: [], |
| b69ab31 | | | 554 | pathconflicts: [], |
| b69ab31 | | | 555 | }, |
| b69ab31 | | | 556 | ]; |
| b69ab31 | | | 557 | |
| b69ab31 | | | 558 | const conflictFileData = (contents: string) => ({ |
| b69ab31 | | | 559 | contents, |
| b69ab31 | | | 560 | exists: true, |
| b69ab31 | | | 561 | isexec: false, |
| b69ab31 | | | 562 | issymlink: false, |
| b69ab31 | | | 563 | }); |
| b69ab31 | | | 564 | const MARK_IN = '<'.repeat(7) + ` dest: aaaaaaaaaaaa - unixname: Commit A`; |
| b69ab31 | | | 565 | const MARK_OUT = '>'.repeat(7) + ` source: bbbbbbbbbbbb - unixname: Commit B`; |
| b69ab31 | | | 566 | const MARK_BASE_START = `||||||| base`; |
| b69ab31 | | | 567 | const MARK_BASE_END = `=======`; |
| b69ab31 | | | 568 | |
| b69ab31 | | | 569 | const MOCK_CONFLICT: ResolveCommandConflictOutput = [ |
| b69ab31 | | | 570 | { |
| b69ab31 | | | 571 | command: 'rebase', |
| b69ab31 | | | 572 | command_details: { |
| b69ab31 | | | 573 | cmd: 'rebase', |
| b69ab31 | | | 574 | to_abort: 'rebase --abort', |
| b69ab31 | | | 575 | to_continue: 'rebase --continue', |
| b69ab31 | | | 576 | }, |
| b69ab31 | | | 577 | conflicts: [ |
| b69ab31 | | | 578 | { |
| b69ab31 | | | 579 | base: conflictFileData('hello\nworld\n'), |
| b69ab31 | | | 580 | local: conflictFileData('hello\nworld - modified 1\n'), |
| b69ab31 | | | 581 | other: conflictFileData('hello\nworld - modified 2\n'), |
| b69ab31 | | | 582 | output: conflictFileData( |
| b69ab31 | | | 583 | `\ |
| b69ab31 | | | 584 | hello |
| b69ab31 | | | 585 | ${MARK_IN} |
| b69ab31 | | | 586 | world - modified 1 |
| b69ab31 | | | 587 | ${MARK_BASE_START} |
| b69ab31 | | | 588 | world |
| b69ab31 | | | 589 | ${MARK_BASE_END} |
| b69ab31 | | | 590 | modified 2 |
| b69ab31 | | | 591 | ${MARK_OUT} |
| b69ab31 | | | 592 | `, |
| b69ab31 | | | 593 | ), |
| b69ab31 | | | 594 | path: 'file1.txt', |
| b69ab31 | | | 595 | }, |
| b69ab31 | | | 596 | { |
| b69ab31 | | | 597 | base: conflictFileData('hello\nworld\n'), |
| b69ab31 | | | 598 | local: conflictFileData('hello\nworld - modified 1\n'), |
| b69ab31 | | | 599 | other: conflictFileData('hello\nworld - modified 2\n'), |
| b69ab31 | | | 600 | output: conflictFileData( |
| b69ab31 | | | 601 | `\ |
| b69ab31 | | | 602 | hello |
| b69ab31 | | | 603 | ${MARK_IN} |
| b69ab31 | | | 604 | world - modified 1 |
| b69ab31 | | | 605 | ${MARK_BASE_START} |
| b69ab31 | | | 606 | world |
| b69ab31 | | | 607 | ${MARK_BASE_END} |
| b69ab31 | | | 608 | modified 2 |
| b69ab31 | | | 609 | ${MARK_OUT} |
| b69ab31 | | | 610 | `, |
| b69ab31 | | | 611 | ), |
| b69ab31 | | | 612 | path: 'file2.txt', |
| b69ab31 | | | 613 | }, |
| b69ab31 | | | 614 | ], |
| b69ab31 | | | 615 | pathconflicts: [], |
| b69ab31 | | | 616 | }, |
| b69ab31 | | | 617 | ]; |
| b69ab31 | | | 618 | |
| b69ab31 | | | 619 | // same as MOCK_CONFLICT, but without any data for file1.txt |
| b69ab31 | | | 620 | const MOCK_CONFLICT_WITH_FILE1_RESOLVED: ResolveCommandConflictOutput = clone(MOCK_CONFLICT); |
| b69ab31 | | | 621 | MOCK_CONFLICT_WITH_FILE1_RESOLVED[0].conflicts.splice(0, 1); |
| b69ab31 | | | 622 | |
| b69ab31 | | | 623 | // these mock values are returned by ejeca / fs mocks |
| b69ab31 | | | 624 | // default: start in a not-in-conflict state |
| b69ab31 | | | 625 | let slMergeDirExists = false; |
| b69ab31 | | | 626 | let conflictData: ResolveCommandConflictOutput = NOT_IN_CONFLICT; |
| b69ab31 | | | 627 | |
| b69ab31 | | | 628 | /** |
| b69ab31 | | | 629 | * the next time repo.checkForMergeConflicts is called, this new conflict data will be used |
| b69ab31 | | | 630 | */ |
| b69ab31 | | | 631 | function enterMergeConflict(conflict: ResolveCommandConflictOutput) { |
| b69ab31 | | | 632 | slMergeDirExists = true; |
| b69ab31 | | | 633 | conflictData = conflict; |
| b69ab31 | | | 634 | } |
| b69ab31 | | | 635 | |
| b69ab31 | | | 636 | beforeEach(() => { |
| b69ab31 | | | 637 | slMergeDirExists = false; |
| b69ab31 | | | 638 | conflictData = NOT_IN_CONFLICT; |
| b69ab31 | | | 639 | |
| b69ab31 | | | 640 | jest.spyOn(fsUtils, 'exists').mockImplementation(() => Promise.resolve(slMergeDirExists)); |
| b69ab31 | | | 641 | |
| b69ab31 | | | 642 | mockEjeca([ |
| b69ab31 | | | 643 | [ |
| b69ab31 | | | 644 | /^sl resolve --tool internal:dumpjson --all/, |
| b69ab31 | | | 645 | () => ({stdout: JSON.stringify(conflictData)}), |
| b69ab31 | | | 646 | ], |
| b69ab31 | | | 647 | ]); |
| b69ab31 | | | 648 | }); |
| b69ab31 | | | 649 | |
| b69ab31 | | | 650 | it('checks for merge conflicts', async () => { |
| b69ab31 | | | 651 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 652 | |
| b69ab31 | | | 653 | const onChange = jest.fn(); |
| b69ab31 | | | 654 | repo.onChangeConflictState(onChange); |
| b69ab31 | | | 655 | |
| b69ab31 | | | 656 | await repo.checkForMergeConflicts(); |
| b69ab31 | | | 657 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 658 | |
| b69ab31 | | | 659 | enterMergeConflict(MOCK_CONFLICT); |
| b69ab31 | | | 660 | |
| b69ab31 | | | 661 | await repo.checkForMergeConflicts(); |
| b69ab31 | | | 662 | |
| b69ab31 | | | 663 | expect(onChange).toHaveBeenCalledWith({state: 'loading'}); |
| b69ab31 | | | 664 | expect(onChange).toHaveBeenCalledWith({ |
| b69ab31 | | | 665 | state: 'loaded', |
| b69ab31 | | | 666 | command: 'rebase', |
| b69ab31 | | | 667 | toContinue: 'rebase --continue', |
| b69ab31 | | | 668 | toAbort: 'rebase --abort', |
| b69ab31 | | | 669 | files: [ |
| b69ab31 | | | 670 | {path: 'file1.txt', status: 'U', conflictType: 'both_changed'}, |
| b69ab31 | | | 671 | {path: 'file2.txt', status: 'U', conflictType: 'both_changed'}, |
| b69ab31 | | | 672 | ], |
| b69ab31 | | | 673 | fetchStartTimestamp: expect.anything(), |
| b69ab31 | | | 674 | fetchCompletedTimestamp: expect.anything(), |
| b69ab31 | | | 675 | } as MergeConflicts); |
| b69ab31 | | | 676 | }); |
| b69ab31 | | | 677 | |
| b69ab31 | | | 678 | it('shows deleted file conflicts', async () => { |
| b69ab31 | | | 679 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 680 | |
| b69ab31 | | | 681 | const onChange = jest.fn(); |
| b69ab31 | | | 682 | repo.onChangeConflictState(onChange); |
| b69ab31 | | | 683 | |
| b69ab31 | | | 684 | await repo.checkForMergeConflicts(); |
| b69ab31 | | | 685 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 686 | |
| b69ab31 | | | 687 | const MOCK_DELETED_CONFLICT: ResolveCommandConflictOutput = [ |
| b69ab31 | | | 688 | { |
| b69ab31 | | | 689 | command: 'rebase', |
| b69ab31 | | | 690 | command_details: { |
| b69ab31 | | | 691 | cmd: 'rebase', |
| b69ab31 | | | 692 | to_abort: 'rebase --abort', |
| b69ab31 | | | 693 | to_continue: 'rebase --continue', |
| b69ab31 | | | 694 | }, |
| b69ab31 | | | 695 | conflicts: [ |
| b69ab31 | | | 696 | { |
| b69ab31 | | | 697 | base: conflictFileData('hello\nworld\n'), |
| b69ab31 | | | 698 | local: conflictFileData('hello\nworld - modified 1\n'), |
| b69ab31 | | | 699 | other: { |
| b69ab31 | | | 700 | contents: null, |
| b69ab31 | | | 701 | exists: false, |
| b69ab31 | | | 702 | isexec: false, |
| b69ab31 | | | 703 | issymlink: false, |
| b69ab31 | | | 704 | }, |
| b69ab31 | | | 705 | output: { |
| b69ab31 | | | 706 | contents: null, |
| b69ab31 | | | 707 | exists: false, |
| b69ab31 | | | 708 | isexec: false, |
| b69ab31 | | | 709 | issymlink: false, |
| b69ab31 | | | 710 | }, |
| b69ab31 | | | 711 | path: 'file_del1.txt', |
| b69ab31 | | | 712 | }, |
| b69ab31 | | | 713 | { |
| b69ab31 | | | 714 | base: conflictFileData('hello\nworld\n'), |
| b69ab31 | | | 715 | local: { |
| b69ab31 | | | 716 | contents: null, |
| b69ab31 | | | 717 | exists: false, |
| b69ab31 | | | 718 | isexec: false, |
| b69ab31 | | | 719 | issymlink: false, |
| b69ab31 | | | 720 | }, |
| b69ab31 | | | 721 | other: conflictFileData('hello\nworld - modified 2\n'), |
| b69ab31 | | | 722 | output: conflictFileData('hello\nworld - modified 2\n'), |
| b69ab31 | | | 723 | path: 'file_del2.txt', |
| b69ab31 | | | 724 | }, |
| b69ab31 | | | 725 | ], |
| b69ab31 | | | 726 | pathconflicts: [], |
| b69ab31 | | | 727 | }, |
| b69ab31 | | | 728 | ]; |
| b69ab31 | | | 729 | |
| b69ab31 | | | 730 | enterMergeConflict(MOCK_DELETED_CONFLICT); |
| b69ab31 | | | 731 | |
| b69ab31 | | | 732 | await repo.checkForMergeConflicts(); |
| b69ab31 | | | 733 | |
| b69ab31 | | | 734 | expect(onChange).toHaveBeenCalledWith({state: 'loading'}); |
| b69ab31 | | | 735 | expect(onChange).toHaveBeenCalledWith({ |
| b69ab31 | | | 736 | state: 'loaded', |
| b69ab31 | | | 737 | command: 'rebase', |
| b69ab31 | | | 738 | toContinue: 'rebase --continue', |
| b69ab31 | | | 739 | toAbort: 'rebase --abort', |
| b69ab31 | | | 740 | files: [ |
| b69ab31 | | | 741 | {path: 'file_del1.txt', status: 'U', conflictType: 'source_deleted'}, |
| b69ab31 | | | 742 | {path: 'file_del2.txt', status: 'U', conflictType: 'dest_deleted'}, |
| b69ab31 | | | 743 | ], |
| b69ab31 | | | 744 | fetchStartTimestamp: expect.anything(), |
| b69ab31 | | | 745 | fetchCompletedTimestamp: expect.anything(), |
| b69ab31 | | | 746 | } as MergeConflicts); |
| b69ab31 | | | 747 | }); |
| b69ab31 | | | 748 | |
| b69ab31 | | | 749 | it('disposes conflict change subscriptions', async () => { |
| b69ab31 | | | 750 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 751 | |
| b69ab31 | | | 752 | const onChange = jest.fn(); |
| b69ab31 | | | 753 | const subscription = repo.onChangeConflictState(onChange); |
| b69ab31 | | | 754 | subscription.dispose(); |
| b69ab31 | | | 755 | |
| b69ab31 | | | 756 | enterMergeConflict(MOCK_CONFLICT); |
| b69ab31 | | | 757 | await repo.checkForMergeConflicts(); |
| b69ab31 | | | 758 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 759 | }); |
| b69ab31 | | | 760 | |
| b69ab31 | | | 761 | it('sends conflicts right away on subscription if already in conflicts', async () => { |
| b69ab31 | | | 762 | enterMergeConflict(MOCK_CONFLICT); |
| b69ab31 | | | 763 | |
| b69ab31 | | | 764 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 765 | |
| b69ab31 | | | 766 | const onChange = jest.fn(); |
| b69ab31 | | | 767 | repo.onChangeConflictState(onChange); |
| b69ab31 | | | 768 | await nextTick(); // allow message to get sent |
| b69ab31 | | | 769 | |
| b69ab31 | | | 770 | expect(onChange).toHaveBeenCalledWith({state: 'loading'}); |
| b69ab31 | | | 771 | expect(onChange).toHaveBeenCalledWith({ |
| b69ab31 | | | 772 | state: 'loaded', |
| b69ab31 | | | 773 | command: 'rebase', |
| b69ab31 | | | 774 | toContinue: 'rebase --continue', |
| b69ab31 | | | 775 | toAbort: 'rebase --abort', |
| b69ab31 | | | 776 | files: [ |
| b69ab31 | | | 777 | {path: 'file1.txt', status: 'U', conflictType: 'both_changed'}, |
| b69ab31 | | | 778 | {path: 'file2.txt', status: 'U', conflictType: 'both_changed'}, |
| b69ab31 | | | 779 | ], |
| b69ab31 | | | 780 | fetchStartTimestamp: expect.anything(), |
| b69ab31 | | | 781 | fetchCompletedTimestamp: expect.anything(), |
| b69ab31 | | | 782 | }); |
| b69ab31 | | | 783 | }); |
| b69ab31 | | | 784 | |
| b69ab31 | | | 785 | it('preserves previous conflicts as resolved', async () => { |
| b69ab31 | | | 786 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 787 | const onChange = jest.fn(); |
| b69ab31 | | | 788 | repo.onChangeConflictState(onChange); |
| b69ab31 | | | 789 | |
| b69ab31 | | | 790 | enterMergeConflict(MOCK_CONFLICT); |
| b69ab31 | | | 791 | await repo.checkForMergeConflicts(); |
| b69ab31 | | | 792 | expect(onChange).toHaveBeenCalledWith({ |
| b69ab31 | | | 793 | state: 'loaded', |
| b69ab31 | | | 794 | command: 'rebase', |
| b69ab31 | | | 795 | toContinue: 'rebase --continue', |
| b69ab31 | | | 796 | toAbort: 'rebase --abort', |
| b69ab31 | | | 797 | files: [ |
| b69ab31 | | | 798 | {path: 'file1.txt', status: 'U', conflictType: 'both_changed'}, |
| b69ab31 | | | 799 | {path: 'file2.txt', status: 'U', conflictType: 'both_changed'}, |
| b69ab31 | | | 800 | ], |
| b69ab31 | | | 801 | fetchStartTimestamp: expect.anything(), |
| b69ab31 | | | 802 | fetchCompletedTimestamp: expect.anything(), |
| b69ab31 | | | 803 | }); |
| b69ab31 | | | 804 | |
| b69ab31 | | | 805 | enterMergeConflict(MOCK_CONFLICT_WITH_FILE1_RESOLVED); |
| b69ab31 | | | 806 | await repo.checkForMergeConflicts(); |
| b69ab31 | | | 807 | expect(onChange).toHaveBeenCalledWith({ |
| b69ab31 | | | 808 | state: 'loaded', |
| b69ab31 | | | 809 | command: 'rebase', |
| b69ab31 | | | 810 | toContinue: 'rebase --continue', |
| b69ab31 | | | 811 | toAbort: 'rebase --abort', |
| b69ab31 | | | 812 | files: [ |
| b69ab31 | | | 813 | // even though file1 is no longer in the output, we remember it from before. |
| b69ab31 | | | 814 | {path: 'file1.txt', status: 'Resolved', conflictType: 'both_changed'}, |
| b69ab31 | | | 815 | {path: 'file2.txt', status: 'U', conflictType: 'both_changed'}, |
| b69ab31 | | | 816 | ], |
| b69ab31 | | | 817 | fetchStartTimestamp: expect.anything(), |
| b69ab31 | | | 818 | fetchCompletedTimestamp: expect.anything(), |
| b69ab31 | | | 819 | }); |
| b69ab31 | | | 820 | }); |
| b69ab31 | | | 821 | |
| b69ab31 | | | 822 | it('handles errors from `sl resolve`', async () => { |
| b69ab31 | | | 823 | mockEjeca([ |
| b69ab31 | | | 824 | [/^sl resolve --tool internal:dumpjson --all/, new Error('failed to do the thing')], |
| b69ab31 | | | 825 | ]); |
| b69ab31 | | | 826 | |
| b69ab31 | | | 827 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 828 | const onChange = jest.fn(); |
| b69ab31 | | | 829 | repo.onChangeConflictState(onChange); |
| b69ab31 | | | 830 | |
| b69ab31 | | | 831 | enterMergeConflict(MOCK_CONFLICT); |
| b69ab31 | | | 832 | await expect(repo.checkForMergeConflicts()).resolves.toEqual(undefined); |
| b69ab31 | | | 833 | |
| b69ab31 | | | 834 | expect(onChange).toHaveBeenCalledWith({state: 'loading'}); |
| b69ab31 | | | 835 | expect(onChange).toHaveBeenCalledWith(undefined); |
| b69ab31 | | | 836 | }); |
| b69ab31 | | | 837 | }); |
| b69ab31 | | | 838 | }); |
| b69ab31 | | | 839 | |
| b69ab31 | | | 840 | describe('extractRepoInfoFromUrl', () => { |
| b69ab31 | | | 841 | describe('github.com', () => { |
| b69ab31 | | | 842 | it('handles http', () => { |
| b69ab31 | | | 843 | expect(extractRepoInfoFromUrl('https://github.com/myUsername/myRepo.git')).toEqual({ |
| b69ab31 | | | 844 | owner: 'myUsername', |
| b69ab31 | | | 845 | repo: 'myRepo', |
| b69ab31 | | | 846 | hostname: 'github.com', |
| b69ab31 | | | 847 | }); |
| b69ab31 | | | 848 | }); |
| b69ab31 | | | 849 | it('handles plain github.com', () => { |
| b69ab31 | | | 850 | expect(extractRepoInfoFromUrl('github.com/myUsername/myRepo.git')).toEqual({ |
| b69ab31 | | | 851 | owner: 'myUsername', |
| b69ab31 | | | 852 | repo: 'myRepo', |
| b69ab31 | | | 853 | hostname: 'github.com', |
| b69ab31 | | | 854 | }); |
| b69ab31 | | | 855 | }); |
| b69ab31 | | | 856 | it('handles git@github', () => { |
| b69ab31 | | | 857 | expect(extractRepoInfoFromUrl('git@github.com:myUsername/myRepo.git')).toEqual({ |
| b69ab31 | | | 858 | owner: 'myUsername', |
| b69ab31 | | | 859 | repo: 'myRepo', |
| b69ab31 | | | 860 | hostname: 'github.com', |
| b69ab31 | | | 861 | }); |
| b69ab31 | | | 862 | }); |
| b69ab31 | | | 863 | it('handles ssh with slashes', () => { |
| b69ab31 | | | 864 | expect(extractRepoInfoFromUrl('ssh://git@github.com/myUsername/my-repo.git')).toEqual({ |
| b69ab31 | | | 865 | owner: 'myUsername', |
| b69ab31 | | | 866 | repo: 'my-repo', |
| b69ab31 | | | 867 | hostname: 'github.com', |
| b69ab31 | | | 868 | }); |
| b69ab31 | | | 869 | }); |
| b69ab31 | | | 870 | it('handles git+ssh', () => { |
| b69ab31 | | | 871 | expect(extractRepoInfoFromUrl('git+ssh://git@github.com:myUsername/myRepo.git')).toEqual({ |
| b69ab31 | | | 872 | owner: 'myUsername', |
| b69ab31 | | | 873 | repo: 'myRepo', |
| b69ab31 | | | 874 | hostname: 'github.com', |
| b69ab31 | | | 875 | }); |
| b69ab31 | | | 876 | }); |
| b69ab31 | | | 877 | it('handles dotted http', () => { |
| b69ab31 | | | 878 | expect(extractRepoInfoFromUrl('https://github.com/myUsername/my.dotted.repo.git')).toEqual({ |
| b69ab31 | | | 879 | owner: 'myUsername', |
| b69ab31 | | | 880 | repo: 'my.dotted.repo', |
| b69ab31 | | | 881 | hostname: 'github.com', |
| b69ab31 | | | 882 | }); |
| b69ab31 | | | 883 | }); |
| b69ab31 | | | 884 | it('handles dotted ssh', () => { |
| b69ab31 | | | 885 | expect(extractRepoInfoFromUrl('git@github.com:myUsername/my.dotted.repo.git')).toEqual({ |
| b69ab31 | | | 886 | owner: 'myUsername', |
| b69ab31 | | | 887 | repo: 'my.dotted.repo', |
| b69ab31 | | | 888 | hostname: 'github.com', |
| b69ab31 | | | 889 | }); |
| b69ab31 | | | 890 | }); |
| b69ab31 | | | 891 | }); |
| b69ab31 | | | 892 | |
| b69ab31 | | | 893 | describe('github enterprise', () => { |
| b69ab31 | | | 894 | it('handles http', () => { |
| b69ab31 | | | 895 | expect(extractRepoInfoFromUrl('https://ghe.company.com/myUsername/myRepo.git')).toEqual({ |
| b69ab31 | | | 896 | owner: 'myUsername', |
| b69ab31 | | | 897 | repo: 'myRepo', |
| b69ab31 | | | 898 | hostname: 'ghe.company.com', |
| b69ab31 | | | 899 | }); |
| b69ab31 | | | 900 | }); |
| b69ab31 | | | 901 | it('handles plain github.com', () => { |
| b69ab31 | | | 902 | expect(extractRepoInfoFromUrl('ghe.company.com/myUsername/myRepo.git')).toEqual({ |
| b69ab31 | | | 903 | owner: 'myUsername', |
| b69ab31 | | | 904 | repo: 'myRepo', |
| b69ab31 | | | 905 | hostname: 'ghe.company.com', |
| b69ab31 | | | 906 | }); |
| b69ab31 | | | 907 | }); |
| b69ab31 | | | 908 | it('handles git@github', () => { |
| b69ab31 | | | 909 | expect(extractRepoInfoFromUrl('git@ghe.company.com:myUsername/myRepo.git')).toEqual({ |
| b69ab31 | | | 910 | owner: 'myUsername', |
| b69ab31 | | | 911 | repo: 'myRepo', |
| b69ab31 | | | 912 | hostname: 'ghe.company.com', |
| b69ab31 | | | 913 | }); |
| b69ab31 | | | 914 | }); |
| b69ab31 | | | 915 | it('handles ssh with slashes', () => { |
| b69ab31 | | | 916 | expect(extractRepoInfoFromUrl('ssh://git@ghe.company.com/myUsername/my-repo.git')).toEqual({ |
| b69ab31 | | | 917 | owner: 'myUsername', |
| b69ab31 | | | 918 | repo: 'my-repo', |
| b69ab31 | | | 919 | hostname: 'ghe.company.com', |
| b69ab31 | | | 920 | }); |
| b69ab31 | | | 921 | }); |
| b69ab31 | | | 922 | it('handles git+ssh', () => { |
| b69ab31 | | | 923 | expect(extractRepoInfoFromUrl('git+ssh://git@ghe.company.com:myUsername/myRepo.git')).toEqual( |
| b69ab31 | | | 924 | { |
| b69ab31 | | | 925 | owner: 'myUsername', |
| b69ab31 | | | 926 | repo: 'myRepo', |
| b69ab31 | | | 927 | hostname: 'ghe.company.com', |
| b69ab31 | | | 928 | }, |
| b69ab31 | | | 929 | ); |
| b69ab31 | | | 930 | }); |
| b69ab31 | | | 931 | it('handles dotted http', () => { |
| b69ab31 | | | 932 | expect( |
| b69ab31 | | | 933 | extractRepoInfoFromUrl('https://ghe.company.com/myUsername/my.dotted.repo.git'), |
| b69ab31 | | | 934 | ).toEqual({ |
| b69ab31 | | | 935 | owner: 'myUsername', |
| b69ab31 | | | 936 | repo: 'my.dotted.repo', |
| b69ab31 | | | 937 | hostname: 'ghe.company.com', |
| b69ab31 | | | 938 | }); |
| b69ab31 | | | 939 | }); |
| b69ab31 | | | 940 | it('handles dotted ssh', () => { |
| b69ab31 | | | 941 | expect(extractRepoInfoFromUrl('git@ghe.company.com:myUsername/my.dotted.repo.git')).toEqual({ |
| b69ab31 | | | 942 | owner: 'myUsername', |
| b69ab31 | | | 943 | repo: 'my.dotted.repo', |
| b69ab31 | | | 944 | hostname: 'ghe.company.com', |
| b69ab31 | | | 945 | }); |
| b69ab31 | | | 946 | }); |
| b69ab31 | | | 947 | }); |
| b69ab31 | | | 948 | }); |
| b69ab31 | | | 949 | |
| b69ab31 | | | 950 | describe('absolutePathForFileInRepo', () => { |
| b69ab31 | | | 951 | let ctx: RepositoryContext; |
| b69ab31 | | | 952 | beforeEach(() => { |
| b69ab31 | | | 953 | ctx = { |
| b69ab31 | | | 954 | cmd: 'sl', |
| b69ab31 | | | 955 | cwd: '/path/to/cwd', |
| b69ab31 | | | 956 | logger: mockLogger, |
| b69ab31 | | | 957 | tracker: mockTracker, |
| b69ab31 | | | 958 | }; |
| b69ab31 | | | 959 | }); |
| b69ab31 | | | 960 | |
| b69ab31 | | | 961 | it('rejects .. in paths that escape the repo', () => { |
| b69ab31 | | | 962 | const repoInfo: ValidatedRepoInfo = { |
| b69ab31 | | | 963 | type: 'success', |
| b69ab31 | | | 964 | command: 'sl', |
| b69ab31 | | | 965 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 966 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 967 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 968 | pullRequestDomain: undefined, |
| b69ab31 | | | 969 | isEdenFs: false, |
| b69ab31 | | | 970 | }; |
| b69ab31 | | | 971 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 972 | |
| b69ab31 | | | 973 | expect(absolutePathForFileInRepo('foo/bar/file.txt', repo)).toEqual( |
| b69ab31 | | | 974 | '/path/to/repo/foo/bar/file.txt', |
| b69ab31 | | | 975 | ); |
| b69ab31 | | | 976 | expect(absolutePathForFileInRepo('foo/../bar/file.txt', repo)).toEqual( |
| b69ab31 | | | 977 | '/path/to/repo/bar/file.txt', |
| b69ab31 | | | 978 | ); |
| b69ab31 | | | 979 | expect(absolutePathForFileInRepo('file.txt', repo)).toEqual('/path/to/repo/file.txt'); |
| b69ab31 | | | 980 | |
| b69ab31 | | | 981 | expect(absolutePathForFileInRepo('/file.txt', repo)).toEqual(null); |
| b69ab31 | | | 982 | expect(absolutePathForFileInRepo('', repo)).toEqual(null); |
| b69ab31 | | | 983 | expect(absolutePathForFileInRepo('foo/../../file.txt', repo)).toEqual(null); |
| b69ab31 | | | 984 | expect(absolutePathForFileInRepo('../file.txt', repo)).toEqual(null); |
| b69ab31 | | | 985 | expect(absolutePathForFileInRepo('/../file.txt', repo)).toEqual(null); |
| b69ab31 | | | 986 | }); |
| b69ab31 | | | 987 | |
| b69ab31 | | | 988 | it('works on windows', () => { |
| b69ab31 | | | 989 | const repoInfo: ValidatedRepoInfo = { |
| b69ab31 | | | 990 | type: 'success', |
| b69ab31 | | | 991 | command: 'sl', |
| b69ab31 | | | 992 | dotdir: 'C:\\path\\to\\repo\\.sl', |
| b69ab31 | | | 993 | repoRoot: 'C:\\path\\to\\repo', |
| b69ab31 | | | 994 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 995 | pullRequestDomain: undefined, |
| b69ab31 | | | 996 | isEdenFs: false, |
| b69ab31 | | | 997 | }; |
| b69ab31 | | | 998 | const repo = new Repository(repoInfo, ctx); |
| b69ab31 | | | 999 | |
| b69ab31 | | | 1000 | expect(absolutePathForFileInRepo('foo\\bar\\file.txt', repo, path.win32)).toEqual( |
| b69ab31 | | | 1001 | 'C:\\path\\to\\repo\\foo\\bar\\file.txt', |
| b69ab31 | | | 1002 | ); |
| b69ab31 | | | 1003 | |
| b69ab31 | | | 1004 | expect(absolutePathForFileInRepo('foo\\..\\..\\file.txt', repo, path.win32)).toEqual(null); |
| b69ab31 | | | 1005 | }); |
| b69ab31 | | | 1006 | }); |
| b69ab31 | | | 1007 | |
| b69ab31 | | | 1008 | describe('getCwdInfo', () => { |
| b69ab31 | | | 1009 | it('computes cwd path and labels', async () => { |
| b69ab31 | | | 1010 | mockEjeca([[/^sl root/, {stdout: '/path/to/myRepo'}]]); |
| b69ab31 | | | 1011 | jest.spyOn(fs.promises, 'realpath').mockImplementation(async (path, _opts) => { |
| b69ab31 | | | 1012 | return path as string; |
| b69ab31 | | | 1013 | }); |
| b69ab31 | | | 1014 | await expect( |
| b69ab31 | | | 1015 | Repository.getCwdInfo({ |
| b69ab31 | | | 1016 | cmd: 'sl', |
| b69ab31 | | | 1017 | cwd: '/path/to/myRepo/some/subdir', |
| b69ab31 | | | 1018 | logger: mockLogger, |
| b69ab31 | | | 1019 | tracker: mockTracker, |
| b69ab31 | | | 1020 | }), |
| b69ab31 | | | 1021 | ).resolves.toEqual({ |
| b69ab31 | | | 1022 | cwd: '/path/to/myRepo/some/subdir', |
| b69ab31 | | | 1023 | repoRoot: '/path/to/myRepo', |
| b69ab31 | | | 1024 | repoRelativeCwdLabel: 'myRepo/some/subdir', |
| b69ab31 | | | 1025 | }); |
| b69ab31 | | | 1026 | }); |
| b69ab31 | | | 1027 | |
| b69ab31 | | | 1028 | it('uses realpath', async () => { |
| b69ab31 | | | 1029 | mockEjeca([[/^sl root/, {stdout: '/data/users/name/myRepo'}]]); |
| b69ab31 | | | 1030 | jest.spyOn(fs.promises, 'realpath').mockImplementation(async (path, _opts) => { |
| b69ab31 | | | 1031 | return (path as string).replace(/^\/home\/name\//, '/data/users/name/'); |
| b69ab31 | | | 1032 | }); |
| b69ab31 | | | 1033 | await expect( |
| b69ab31 | | | 1034 | Repository.getCwdInfo({ |
| b69ab31 | | | 1035 | cmd: 'sl', |
| b69ab31 | | | 1036 | cwd: '/home/name/myRepo/some/subdir', |
| b69ab31 | | | 1037 | logger: mockLogger, |
| b69ab31 | | | 1038 | tracker: mockTracker, |
| b69ab31 | | | 1039 | }), |
| b69ab31 | | | 1040 | ).resolves.toEqual({ |
| b69ab31 | | | 1041 | cwd: '/home/name/myRepo/some/subdir', // cwd is not realpath'd |
| b69ab31 | | | 1042 | repoRoot: '/data/users/name/myRepo', // repo root is realpath'd |
| b69ab31 | | | 1043 | repoRelativeCwdLabel: 'myRepo/some/subdir', |
| b69ab31 | | | 1044 | }); |
| b69ab31 | | | 1045 | }); |
| b69ab31 | | | 1046 | |
| b69ab31 | | | 1047 | it('returns null for non-repos', async () => { |
| b69ab31 | | | 1048 | mockEjeca([[/^sl root/, new Error('not a repository')]]); |
| b69ab31 | | | 1049 | await expect( |
| b69ab31 | | | 1050 | Repository.getCwdInfo({ |
| b69ab31 | | | 1051 | cmd: 'sl', |
| b69ab31 | | | 1052 | cwd: '/path/ro/myRepo/some/subdir', |
| b69ab31 | | | 1053 | logger: mockLogger, |
| b69ab31 | | | 1054 | tracker: mockTracker, |
| b69ab31 | | | 1055 | }), |
| b69ab31 | | | 1056 | ).resolves.toEqual({ |
| b69ab31 | | | 1057 | cwd: '/path/ro/myRepo/some/subdir', |
| b69ab31 | | | 1058 | }); |
| b69ab31 | | | 1059 | }); |
| b69ab31 | | | 1060 | }); |
| b69ab31 | | | 1061 | |
| b69ab31 | | | 1062 | describe('fetchSubmoduleMap', () => { |
| b69ab31 | | | 1063 | let myRepoRoot: AbsolutePath; |
| b69ab31 | | | 1064 | let ctx: RepositoryContext; |
| b69ab31 | | | 1065 | beforeEach(() => { |
| b69ab31 | | | 1066 | myRepoRoot = '/data/users/name/myRepo'; |
| b69ab31 | | | 1067 | ctx = { |
| b69ab31 | | | 1068 | cmd: 'sl', |
| b69ab31 | | | 1069 | cwd: myRepoRoot, |
| b69ab31 | | | 1070 | logger: mockLogger, |
| b69ab31 | | | 1071 | tracker: mockTracker, |
| b69ab31 | | | 1072 | }; |
| b69ab31 | | | 1073 | }); |
| b69ab31 | | | 1074 | |
| b69ab31 | | | 1075 | it('simple', async () => { |
| b69ab31 | | | 1076 | const submodules: Submodule[] = [ |
| b69ab31 | | | 1077 | { |
| b69ab31 | | | 1078 | name: 'submoduleA', |
| b69ab31 | | | 1079 | url: 'https://ghe.myCompany.com/myUsername/myRepo/submoduleA', |
| b69ab31 | | | 1080 | path: 'submoduleA', |
| b69ab31 | | | 1081 | active: true, |
| b69ab31 | | | 1082 | }, |
| b69ab31 | | | 1083 | { |
| b69ab31 | | | 1084 | name: 'submoduleB', |
| b69ab31 | | | 1085 | url: 'https://ghe.myCompany.com/myUsername/myRepo/submoduleB', |
| b69ab31 | | | 1086 | path: 'submoduleB', |
| b69ab31 | | | 1087 | active: false, |
| b69ab31 | | | 1088 | }, |
| b69ab31 | | | 1089 | ]; |
| b69ab31 | | | 1090 | const submodulesJson = JSON.stringify(submodules); |
| b69ab31 | | | 1091 | mockEjeca([ |
| b69ab31 | | | 1092 | [/^sl root/, {stdout: myRepoRoot}], |
| b69ab31 | | | 1093 | [/^sl debugroots/, {stdout: myRepoRoot}], |
| b69ab31 | | | 1094 | [/^sl debuggitmodules/, {stdout: submodulesJson}], |
| b69ab31 | | | 1095 | ]); |
| b69ab31 | | | 1096 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 1097 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 1098 | await repo.fetchSubmoduleMap(); |
| b69ab31 | | | 1099 | const fetchedSubmoduleMap = repo.getSubmoduleMap(); |
| b69ab31 | | | 1100 | expect(fetchedSubmoduleMap).not.toBeUndefined(); |
| b69ab31 | | | 1101 | expect(fetchedSubmoduleMap?.get(myRepoRoot)?.value).toEqual(submodules); |
| b69ab31 | | | 1102 | }); |
| b69ab31 | | | 1103 | |
| b69ab31 | | | 1104 | it('no submodules', async () => { |
| b69ab31 | | | 1105 | const submodules: Submodule[] = []; |
| b69ab31 | | | 1106 | const submodulesJson = JSON.stringify(submodules); |
| b69ab31 | | | 1107 | mockEjeca([ |
| b69ab31 | | | 1108 | [/^sl root/, {stdout: myRepoRoot}], |
| b69ab31 | | | 1109 | [/^sl debugroots/, {stdout: myRepoRoot}], |
| b69ab31 | | | 1110 | [/^sl debuggitmodules/, {stdout: submodulesJson}], |
| b69ab31 | | | 1111 | ]); |
| b69ab31 | | | 1112 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 1113 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 1114 | await repo.fetchSubmoduleMap(); |
| b69ab31 | | | 1115 | const fetchedSubmoduleMap = repo.getSubmoduleMap(); |
| b69ab31 | | | 1116 | expect(fetchedSubmoduleMap).not.toBeUndefined(); |
| b69ab31 | | | 1117 | expect(fetchedSubmoduleMap?.get(myRepoRoot)?.value).toBeUndefined(); |
| b69ab31 | | | 1118 | }); |
| b69ab31 | | | 1119 | |
| b69ab31 | | | 1120 | it('nested', async () => { |
| b69ab31 | | | 1121 | const submodulesOfMyRepo: Submodule[] = [ |
| b69ab31 | | | 1122 | { |
| b69ab31 | | | 1123 | name: 'submoduleA', |
| b69ab31 | | | 1124 | url: 'https://ghe.myCompany.com/myUsername/myRepo/submoduleA', |
| b69ab31 | | | 1125 | path: 'submoduleA', |
| b69ab31 | | | 1126 | active: true, |
| b69ab31 | | | 1127 | }, |
| b69ab31 | | | 1128 | ]; |
| b69ab31 | | | 1129 | const submoduleARoot = myRepoRoot + '/submoduleA'; |
| b69ab31 | | | 1130 | const submodulesOfA: Submodule[] = [ |
| b69ab31 | | | 1131 | { |
| b69ab31 | | | 1132 | name: 'submoduleB', |
| b69ab31 | | | 1133 | url: 'https://ghe.myCompany.com/myUsername/myRepo/submoduleA/submoduleB', |
| b69ab31 | | | 1134 | path: 'submoduleB', |
| b69ab31 | | | 1135 | active: true, |
| b69ab31 | | | 1136 | }, |
| b69ab31 | | | 1137 | ]; |
| b69ab31 | | | 1138 | const submoduleBRoot = submoduleARoot + '/submoduleB'; |
| b69ab31 | | | 1139 | mockEjeca([ |
| b69ab31 | | | 1140 | [ |
| b69ab31 | | | 1141 | new RegExp(`^sl debuggitmodules --json --repo ${submoduleARoot}`), |
| b69ab31 | | | 1142 | {stdout: JSON.stringify(submodulesOfA)}, |
| b69ab31 | | | 1143 | ], |
| b69ab31 | | | 1144 | [ |
| b69ab31 | | | 1145 | new RegExp(`^sl debuggitmodules --json --repo ${myRepoRoot}`), |
| b69ab31 | | | 1146 | {stdout: JSON.stringify(submodulesOfMyRepo)}, |
| b69ab31 | | | 1147 | ], |
| b69ab31 | | | 1148 | [/^sl root/, {stdout: submoduleBRoot}], |
| b69ab31 | | | 1149 | [/^sl debugroots/, {stdout: myRepoRoot + '\n' + submoduleARoot + '\n' + submoduleBRoot}], |
| b69ab31 | | | 1150 | ]); |
| b69ab31 | | | 1151 | const updatedCtx = {...ctx, cwd: submoduleBRoot}; |
| b69ab31 | | | 1152 | const info = (await Repository.getRepoInfo(updatedCtx)) as ValidatedRepoInfo; |
| b69ab31 | | | 1153 | const repo = new Repository(info, updatedCtx); |
| b69ab31 | | | 1154 | await repo.fetchSubmoduleMap(); |
| b69ab31 | | | 1155 | const fetchedSubmoduleMap = repo.getSubmoduleMap(); |
| b69ab31 | | | 1156 | |
| b69ab31 | | | 1157 | expect(fetchedSubmoduleMap).not.toBeUndefined(); |
| b69ab31 | | | 1158 | expect(fetchedSubmoduleMap?.get(myRepoRoot)?.value).toEqual(submodulesOfMyRepo); |
| b69ab31 | | | 1159 | expect(fetchedSubmoduleMap?.get(submoduleARoot)?.value).toEqual(submodulesOfA); |
| b69ab31 | | | 1160 | }); |
| b69ab31 | | | 1161 | |
| b69ab31 | | | 1162 | it('error', async () => { |
| b69ab31 | | | 1163 | const msg = 'mock sapling error'; |
| b69ab31 | | | 1164 | mockEjeca([ |
| b69ab31 | | | 1165 | [/^sl root/, {stdout: myRepoRoot}], |
| b69ab31 | | | 1166 | [/^sl debugroots/, {stdout: myRepoRoot}], |
| b69ab31 | | | 1167 | [/^sl debuggitmodules/, new Error(msg)], |
| b69ab31 | | | 1168 | ]); |
| b69ab31 | | | 1169 | const info = (await Repository.getRepoInfo(ctx)) as ValidatedRepoInfo; |
| b69ab31 | | | 1170 | const repo = new Repository(info, ctx); |
| b69ab31 | | | 1171 | await repo.fetchSubmoduleMap(); |
| b69ab31 | | | 1172 | const fetchedSubmoduleMap = repo.getSubmoduleMap(); |
| b69ab31 | | | 1173 | |
| b69ab31 | | | 1174 | expect(fetchedSubmoduleMap).not.toBeUndefined(); |
| b69ab31 | | | 1175 | expect(fetchedSubmoduleMap?.get(myRepoRoot)?.value).toBeUndefined(); |
| b69ab31 | | | 1176 | expect(fetchedSubmoduleMap?.get(myRepoRoot)?.error?.message).toMatch(msg); |
| b69ab31 | | | 1177 | }); |
| b69ab31 | | | 1178 | }); |