| 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 {RepoInfo, RepositoryError, ValidatedRepoInfo} from 'isl/src/types'; |
| b69ab31 | | | 9 | import type {Repository} from '../Repository'; |
| b69ab31 | | | 10 | import type {Logger} from '../logger'; |
| b69ab31 | | | 11 | import type {ServerPlatform} from '../serverPlatform'; |
| b69ab31 | | | 12 | import type {RepositoryContext} from '../serverTypes'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import {ensureTrailingPathSep} from 'shared/pathUtils'; |
| b69ab31 | | | 15 | import {mockLogger} from 'shared/testUtils'; |
| b69ab31 | | | 16 | import {defer} from 'shared/utils'; |
| b69ab31 | | | 17 | import {__TEST__} from '../RepositoryCache'; |
| b69ab31 | | | 18 | import {makeServerSideTracker} from '../analytics/serverSideTracker'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | const {RepositoryCache, RepoMap, RefCounted} = __TEST__; |
| b69ab31 | | | 21 | |
| b69ab31 | | | 22 | const mockTracker = makeServerSideTracker( |
| b69ab31 | | | 23 | mockLogger, |
| b69ab31 | | | 24 | {platformName: 'test'} as ServerPlatform, |
| b69ab31 | | | 25 | '0.1', |
| b69ab31 | | | 26 | jest.fn(), |
| b69ab31 | | | 27 | ); |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | class SimpleMockRepositoryImpl { |
| b69ab31 | | | 30 | static getRepoInfo(ctx: RepositoryContext): Promise<RepoInfo> { |
| b69ab31 | | | 31 | const {cwd, cmd} = ctx; |
| b69ab31 | | | 32 | let data; |
| b69ab31 | | | 33 | if (cwd.includes('/path/to/repo')) { |
| b69ab31 | | | 34 | data = { |
| b69ab31 | | | 35 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 36 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 37 | }; |
| b69ab31 | | | 38 | } else if (cwd.includes('/path/to/anotherrepo')) { |
| b69ab31 | | | 39 | data = { |
| b69ab31 | | | 40 | repoRoot: '/path/to/anotherrepo', |
| b69ab31 | | | 41 | dotdir: '/path/to/anotherrepo/.sl', |
| b69ab31 | | | 42 | }; |
| b69ab31 | | | 43 | } else if (cwd.includes('/path/to/submodule')) { |
| b69ab31 | | | 44 | data = { |
| b69ab31 | | | 45 | repoRoot: cwd.endsWith('/cwd') ? cwd.slice(0, -4) : cwd, |
| b69ab31 | | | 46 | dotdir: ensureTrailingPathSep(cwd) + '.sl', |
| b69ab31 | | | 47 | }; |
| b69ab31 | | | 48 | } else { |
| b69ab31 | | | 49 | return Promise.resolve({type: 'cwdNotARepository', cwd} as RepositoryError); |
| b69ab31 | | | 50 | } |
| b69ab31 | | | 51 | return Promise.resolve({ |
| b69ab31 | | | 52 | type: 'success', |
| b69ab31 | | | 53 | command: cmd, |
| b69ab31 | | | 54 | pullRequestDomain: undefined, |
| b69ab31 | | | 55 | preferredSubmitCommand: 'pr', |
| b69ab31 | | | 56 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 57 | isEdenFs: false, |
| b69ab31 | | | 58 | ...data, |
| b69ab31 | | | 59 | }); |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | constructor( |
| b69ab31 | | | 62 | public info: RepoInfo, |
| b69ab31 | | | 63 | public logger: Logger, |
| b69ab31 | | | 64 | ) {} |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | dispose = jest.fn(); |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | const SimpleMockRepository = SimpleMockRepositoryImpl as unknown as typeof Repository; |
| b69ab31 | | | 69 | |
| b69ab31 | | | 70 | const ctx: RepositoryContext = { |
| b69ab31 | | | 71 | cmd: 'sl', |
| b69ab31 | | | 72 | logger: mockLogger, |
| b69ab31 | | | 73 | tracker: mockTracker, |
| b69ab31 | | | 74 | cwd: '/path/to/repo/cwd', |
| b69ab31 | | | 75 | }; |
| b69ab31 | | | 76 | |
| b69ab31 | | | 77 | describe('RepositoryCache', () => { |
| b69ab31 | | | 78 | it('Provides repository references that resolve', async () => { |
| b69ab31 | | | 79 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 80 | const ref = cache.getOrCreate(ctx); |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | const repo = await ref.promise; |
| b69ab31 | | | 83 | expect(repo).toEqual( |
| b69ab31 | | | 84 | expect.objectContaining({ |
| b69ab31 | | | 85 | info: expect.objectContaining({ |
| b69ab31 | | | 86 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 87 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 88 | }), |
| b69ab31 | | | 89 | }), |
| b69ab31 | | | 90 | ); |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | ref.unref(); |
| b69ab31 | | | 93 | }); |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | it('Gives error for paths without repos', async () => { |
| b69ab31 | | | 96 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 97 | const ref = cache.getOrCreate({...ctx, cwd: '/some/invalid/repo'}); |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | const repo = await ref.promise; |
| b69ab31 | | | 100 | expect(repo).toEqual({type: 'cwdNotARepository', cwd: '/some/invalid/repo'}); |
| b69ab31 | | | 101 | |
| b69ab31 | | | 102 | ref.unref(); |
| b69ab31 | | | 103 | }); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | it('Disposes repositories', async () => { |
| b69ab31 | | | 106 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 107 | const ref = cache.getOrCreate(ctx); |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | const repo = await ref.promise; |
| b69ab31 | | | 110 | const disposeFunc = (repo as Repository).dispose; |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | expect(cache.numberOfActiveServers()).toBe(1); |
| b69ab31 | | | 113 | |
| b69ab31 | | | 114 | ref.unref(); |
| b69ab31 | | | 115 | expect(disposeFunc).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 116 | expect(cache.numberOfActiveServers()).toBe(0); |
| b69ab31 | | | 117 | }); |
| b69ab31 | | | 118 | |
| b69ab31 | | | 119 | it('Can dispose references before the repo promise has resolved', async () => { |
| b69ab31 | | | 120 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 121 | const ref = cache.getOrCreate(ctx); |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | ref.unref(); |
| b69ab31 | | | 124 | |
| b69ab31 | | | 125 | const repo = await ref.promise; |
| b69ab31 | | | 126 | // even though this would be a valid repo, by disposing the ref before it is created, |
| b69ab31 | | | 127 | // we prevent creating a repo. |
| b69ab31 | | | 128 | expect(repo).toEqual({type: 'unknownError', error: expect.anything()}); |
| b69ab31 | | | 129 | }); |
| b69ab31 | | | 130 | |
| b69ab31 | | | 131 | it('shares repositories under the same cwd', async () => { |
| b69ab31 | | | 132 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 133 | const ref1 = cache.getOrCreate({...ctx, cwd: '/path/to/repo'}); |
| b69ab31 | | | 134 | const repo1 = await ref1.promise; |
| b69ab31 | | | 135 | |
| b69ab31 | | | 136 | const ref2 = cache.getOrCreate({...ctx, cwd: '/path/to/repo'}); |
| b69ab31 | | | 137 | const repo2 = await ref2.promise; |
| b69ab31 | | | 138 | |
| b69ab31 | | | 139 | expect(cache.numberOfActiveServers()).toBe(2); |
| b69ab31 | | | 140 | |
| b69ab31 | | | 141 | expect(repo1).toBe(repo2); |
| b69ab31 | | | 142 | |
| b69ab31 | | | 143 | ref1.unref(); |
| b69ab31 | | | 144 | ref2.unref(); |
| b69ab31 | | | 145 | }); |
| b69ab31 | | | 146 | |
| b69ab31 | | | 147 | it('shares repositories under the same repo', async () => { |
| b69ab31 | | | 148 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 149 | const ref1 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd1'}); |
| b69ab31 | | | 150 | const repo1 = await ref1.promise; |
| b69ab31 | | | 151 | |
| b69ab31 | | | 152 | const ref2 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd2'}); |
| b69ab31 | | | 153 | const repo2 = await ref2.promise; |
| b69ab31 | | | 154 | |
| b69ab31 | | | 155 | expect(cache.numberOfActiveServers()).toBe(2); |
| b69ab31 | | | 156 | |
| b69ab31 | | | 157 | expect(repo1).toBe(repo2); |
| b69ab31 | | | 158 | |
| b69ab31 | | | 159 | ref1.unref(); |
| b69ab31 | | | 160 | ref2.unref(); |
| b69ab31 | | | 161 | }); |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | it('does not share repositories under different cwds', async () => { |
| b69ab31 | | | 164 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 165 | const ref1 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd'}); |
| b69ab31 | | | 166 | const ref2 = cache.getOrCreate({...ctx, cwd: '/path/to/anotherrepo/cwd'}); |
| b69ab31 | | | 167 | |
| b69ab31 | | | 168 | const repo1 = await ref1.promise; |
| b69ab31 | | | 169 | const repo2 = await ref2.promise; |
| b69ab31 | | | 170 | |
| b69ab31 | | | 171 | expect(repo1).not.toBe(repo2); |
| b69ab31 | | | 172 | |
| b69ab31 | | | 173 | ref1.unref(); |
| b69ab31 | | | 174 | ref2.unref(); |
| b69ab31 | | | 175 | }); |
| b69ab31 | | | 176 | |
| b69ab31 | | | 177 | it('reference counts and disposes after 0 refs', async () => { |
| b69ab31 | | | 178 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 179 | const ref1 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd1'}); |
| b69ab31 | | | 180 | const ref2 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd2'}); |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | const repo = await ref1.promise; |
| b69ab31 | | | 183 | await ref2.promise; |
| b69ab31 | | | 184 | expect(cache.numberOfActiveServers()).toBe(2); |
| b69ab31 | | | 185 | |
| b69ab31 | | | 186 | const disposeFunc = (repo as Repository).dispose; |
| b69ab31 | | | 187 | |
| b69ab31 | | | 188 | ref1.unref(); |
| b69ab31 | | | 189 | expect(disposeFunc).not.toHaveBeenCalled(); |
| b69ab31 | | | 190 | ref2.unref(); |
| b69ab31 | | | 191 | expect(disposeFunc).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 192 | |
| b69ab31 | | | 193 | expect(cache.numberOfActiveServers()).toBe(0); |
| b69ab31 | | | 194 | }); |
| b69ab31 | | | 195 | |
| b69ab31 | | | 196 | it('does not reuse disposed repos', async () => { |
| b69ab31 | | | 197 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 198 | const ref1 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd1'}); |
| b69ab31 | | | 199 | |
| b69ab31 | | | 200 | const repo1 = await ref1.promise; |
| b69ab31 | | | 201 | ref1.unref(); |
| b69ab31 | | | 202 | |
| b69ab31 | | | 203 | const ref2 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd2'}); |
| b69ab31 | | | 204 | const repo2 = await ref2.promise; |
| b69ab31 | | | 205 | |
| b69ab31 | | | 206 | expect(cache.numberOfActiveServers()).toBe(1); |
| b69ab31 | | | 207 | |
| b69ab31 | | | 208 | expect(repo1).not.toBe(repo2); |
| b69ab31 | | | 209 | expect((repo1 as Repository).dispose).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 210 | expect((repo2 as Repository).dispose).not.toHaveBeenCalled(); |
| b69ab31 | | | 211 | |
| b69ab31 | | | 212 | ref2.unref(); |
| b69ab31 | | | 213 | }); |
| b69ab31 | | | 214 | |
| b69ab31 | | | 215 | it('prefix matching repos are treated separately', async () => { |
| b69ab31 | | | 216 | const cache = new RepositoryCache(SimpleMockRepository); |
| b69ab31 | | | 217 | const ref1 = cache.getOrCreate({...ctx, cwd: '/path/to/repo'}); |
| b69ab31 | | | 218 | await ref1.promise; |
| b69ab31 | | | 219 | |
| b69ab31 | | | 220 | expect(cache.cachedRepositoryForPath('/path/to/repo')).not.toEqual(undefined); |
| b69ab31 | | | 221 | expect(cache.cachedRepositoryForPath('/path/to/repo/')).not.toEqual(undefined); |
| b69ab31 | | | 222 | expect(cache.cachedRepositoryForPath('/path/to/repo/foo')).not.toEqual(undefined); |
| b69ab31 | | | 223 | // this is actually different repo |
| b69ab31 | | | 224 | expect(cache.cachedRepositoryForPath('/path/to/repo-1')).toEqual(undefined); |
| b69ab31 | | | 225 | |
| b69ab31 | | | 226 | ref1.unref(); |
| b69ab31 | | | 227 | expect(cache.cachedRepositoryForPath('/path/to/repo')).toEqual(undefined); |
| b69ab31 | | | 228 | |
| b69ab31 | | | 229 | // Test longest prefix match for nested repos/submodules |
| b69ab31 | | | 230 | const refSubmodule = cache.getOrCreate({...ctx, cwd: '/path/to/submodule'}); |
| b69ab31 | | | 231 | await refSubmodule.promise; |
| b69ab31 | | | 232 | const refSubmoduleNested = cache.getOrCreate({...ctx, cwd: '/path/to/submodule/nested'}); |
| b69ab31 | | | 233 | await refSubmoduleNested.promise; |
| b69ab31 | | | 234 | |
| b69ab31 | | | 235 | const repoSubmodule = cache.cachedRepositoryForPath('/path/to/submodule'); |
| b69ab31 | | | 236 | const repoSubmoduleFoo = cache.cachedRepositoryForPath('/path/to/submodule/foo'); |
| b69ab31 | | | 237 | const repoNested = cache.cachedRepositoryForPath('/path/to/submodule/nested'); |
| b69ab31 | | | 238 | const repoNestedBar = cache.cachedRepositoryForPath('/path/to/submodule/nested/bar'); |
| b69ab31 | | | 239 | |
| b69ab31 | | | 240 | expect(repoSubmodule?.info.repoRoot).toEqual('/path/to/submodule'); |
| b69ab31 | | | 241 | expect(repoSubmoduleFoo).toEqual(repoSubmodule); |
| b69ab31 | | | 242 | expect(repoNested?.info.repoRoot).toEqual('/path/to/submodule/nested'); |
| b69ab31 | | | 243 | expect(repoNestedBar).toEqual(repoNested); |
| b69ab31 | | | 244 | |
| b69ab31 | | | 245 | refSubmoduleNested.unref(); |
| b69ab31 | | | 246 | expect(cache.cachedRepositoryForPath('/path/to/submodule/nested')).toEqual(undefined); |
| b69ab31 | | | 247 | expect(cache.cachedRepositoryForPath('/path/to/submodule')).not.toEqual(undefined); |
| b69ab31 | | | 248 | |
| b69ab31 | | | 249 | refSubmodule.unref(); |
| b69ab31 | | | 250 | expect(cache.cachedRepositoryForPath('/path/to/submodule')).toEqual(undefined); |
| b69ab31 | | | 251 | }); |
| b69ab31 | | | 252 | |
| b69ab31 | | | 253 | it('only creates one Repository even when racing lookups', async () => { |
| b69ab31 | | | 254 | const repoInfo = { |
| b69ab31 | | | 255 | type: 'success', |
| b69ab31 | | | 256 | command: 'sl', |
| b69ab31 | | | 257 | pullRequestDomain: undefined, |
| b69ab31 | | | 258 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 259 | repoRoot: '/path/to/repo', |
| b69ab31 | | | 260 | dotdir: '/path/to/repo/.sl', |
| b69ab31 | | | 261 | isEdenFs: false, |
| b69ab31 | | | 262 | } as RepoInfo; |
| b69ab31 | | | 263 | |
| b69ab31 | | | 264 | // two different fake async fetches for RepoInfo |
| b69ab31 | | | 265 | const p1 = defer<RepoInfo>(); |
| b69ab31 | | | 266 | const p2 = defer<RepoInfo>(); |
| b69ab31 | | | 267 | |
| b69ab31 | | | 268 | class BlockingMockRepository { |
| b69ab31 | | | 269 | static getRepoInfo(ctx: RepositoryContext): Promise<RepoInfo> { |
| b69ab31 | | | 270 | const {cwd} = ctx; |
| b69ab31 | | | 271 | if (cwd === '/path/to/repo/cwd1') { |
| b69ab31 | | | 272 | return p1.promise; |
| b69ab31 | | | 273 | } else if (cwd === '/path/to/repo/cwd2') { |
| b69ab31 | | | 274 | return p2.promise; |
| b69ab31 | | | 275 | } |
| b69ab31 | | | 276 | throw new Error('unknown repo'); |
| b69ab31 | | | 277 | } |
| b69ab31 | | | 278 | constructor( |
| b69ab31 | | | 279 | public info: RepoInfo, |
| b69ab31 | | | 280 | public logger: Logger, |
| b69ab31 | | | 281 | ) {} |
| b69ab31 | | | 282 | |
| b69ab31 | | | 283 | dispose = jest.fn(); |
| b69ab31 | | | 284 | } |
| b69ab31 | | | 285 | |
| b69ab31 | | | 286 | const cache = new RepositoryCache(BlockingMockRepository as unknown as typeof Repository); |
| b69ab31 | | | 287 | // start looking up repoInfos at the same time |
| b69ab31 | | | 288 | const ref1 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd1'}); |
| b69ab31 | | | 289 | const ref2 = cache.getOrCreate({...ctx, cwd: '/path/to/repo/cwd2'}); |
| b69ab31 | | | 290 | |
| b69ab31 | | | 291 | p2.resolve({...repoInfo}); |
| b69ab31 | | | 292 | const repo2 = await ref2.promise; |
| b69ab31 | | | 293 | |
| b69ab31 | | | 294 | p1.resolve({...repoInfo}); |
| b69ab31 | | | 295 | const repo1 = await ref1.promise; |
| b69ab31 | | | 296 | |
| b69ab31 | | | 297 | // we end up with the same repo |
| b69ab31 | | | 298 | expect(repo1).toBe(repo2); |
| b69ab31 | | | 299 | |
| b69ab31 | | | 300 | ref1.unref(); |
| b69ab31 | | | 301 | ref2.unref(); |
| b69ab31 | | | 302 | }); |
| b69ab31 | | | 303 | }); |
| b69ab31 | | | 304 | |
| b69ab31 | | | 305 | describe('RepoMap', () => { |
| b69ab31 | | | 306 | it('iteration with values() and forEach()', async () => { |
| b69ab31 | | | 307 | const repoNum = 10; |
| b69ab31 | | | 308 | const repoRoots = []; |
| b69ab31 | | | 309 | const promises = []; |
| b69ab31 | | | 310 | const createRefCountedRepo = async (ctx: RepositoryContext) => { |
| b69ab31 | | | 311 | const repoInfo = await SimpleMockRepository.getRepoInfo(ctx); |
| b69ab31 | | | 312 | const repo = new SimpleMockRepository(repoInfo as ValidatedRepoInfo, ctx); |
| b69ab31 | | | 313 | return new RefCounted(repo); |
| b69ab31 | | | 314 | }; |
| b69ab31 | | | 315 | for (let i = 0; i < repoNum; i++) { |
| b69ab31 | | | 316 | repoRoots.push(`/path/to/submodule${i}`); |
| b69ab31 | | | 317 | promises.push(createRefCountedRepo({...ctx, cwd: `/path/to/submodule${i}`})); |
| b69ab31 | | | 318 | } |
| b69ab31 | | | 319 | const repos = await Promise.all(promises); |
| b69ab31 | | | 320 | |
| b69ab31 | | | 321 | const repoMap = new RepoMap(); |
| b69ab31 | | | 322 | for (let i = 0; i < repoNum; i++) { |
| b69ab31 | | | 323 | repos[i].ref(); |
| b69ab31 | | | 324 | repoMap.set(repoRoots[i], repos[i]); |
| b69ab31 | | | 325 | } |
| b69ab31 | | | 326 | |
| b69ab31 | | | 327 | const values = [...repoMap.values()]; |
| b69ab31 | | | 328 | expect(values.length).toBe(repoNum); |
| b69ab31 | | | 329 | for (let i = 0; i < repoNum; i++) { |
| b69ab31 | | | 330 | expect(values[i].value.info.repoRoot).toBe(repoRoots[i]); |
| b69ab31 | | | 331 | } |
| b69ab31 | | | 332 | |
| b69ab31 | | | 333 | repoMap.forEach(repo => expect(repo.getNumberOfReferences()).toBe(1)); |
| b69ab31 | | | 334 | repoMap.forEach(repo => repo.dispose()); |
| b69ab31 | | | 335 | repoMap.forEach(repo => expect(repo.getNumberOfReferences()).toBe(0)); |
| b69ab31 | | | 336 | }); |
| b69ab31 | | | 337 | |
| b69ab31 | | | 338 | it('longest prefix match', async () => { |
| b69ab31 | | | 339 | const createRefCountedRepo = async (ctx: RepositoryContext) => { |
| b69ab31 | | | 340 | const repoInfo = await SimpleMockRepository.getRepoInfo(ctx); |
| b69ab31 | | | 341 | const repo = new SimpleMockRepository(repoInfo as ValidatedRepoInfo, ctx); |
| b69ab31 | | | 342 | return new RefCounted(repo); |
| b69ab31 | | | 343 | }; |
| b69ab31 | | | 344 | const a = await createRefCountedRepo({...ctx, cwd: '/path/to/submoduleA/cwd'}); |
| b69ab31 | | | 345 | const b = await createRefCountedRepo({...ctx, cwd: '/path/to/submoduleB/cwd'}); |
| b69ab31 | | | 346 | const nested = await createRefCountedRepo({ |
| b69ab31 | | | 347 | ...ctx, |
| b69ab31 | | | 348 | cwd: '/path/to/submoduleA/submoduleNested/cwd', |
| b69ab31 | | | 349 | }); |
| b69ab31 | | | 350 | |
| b69ab31 | | | 351 | const repoMap = new RepoMap(); |
| b69ab31 | | | 352 | // A raw map would iterate in insertion order, so we |
| b69ab31 | | | 353 | // call set in reverse order to test longest prefix match |
| b69ab31 | | | 354 | repoMap.set('/path/to/submoduleA/submoduleNested', nested); |
| b69ab31 | | | 355 | repoMap.set('/path/to/submoduleB', b); |
| b69ab31 | | | 356 | repoMap.set('/path/to/submoduleA', a); |
| b69ab31 | | | 357 | |
| b69ab31 | | | 358 | expect(repoMap.get('/path/to/submoduleA')).toBe(a); |
| b69ab31 | | | 359 | expect(repoMap.get('/path/to/submoduleA/some/dir')).toBeUndefined(); |
| b69ab31 | | | 360 | expect(repoMap.getLongestPrefixMatch('/path/to/submoduleA/some/dir')).toBe(a); |
| b69ab31 | | | 361 | expect(repoMap.getLongestPrefixMatch('/path/to/submoduleB/some/dir')).toBe(b); |
| b69ab31 | | | 362 | expect(repoMap.getLongestPrefixMatch('/path/to/submoduleA/submoduleNested/dir')).toBe(nested); |
| b69ab31 | | | 363 | }); |
| b69ab31 | | | 364 | }); |