|
1 | | -import { readFileSync } from 'fs'; |
2 | | -import { logging } from '@angular-devkit/core'; |
| 1 | +import { mkdirSync, mkdtempSync, readFileSync, rmdirSync, writeFileSync } from 'fs'; |
| 2 | +import { tmpdir } from 'os'; |
| 3 | +import { join } from 'path'; |
| 4 | +import { logging, normalize, virtualFs } from '@angular-devkit/core'; |
| 5 | +import { NodeJsSyncHost } from '@angular-devkit/core/node'; |
3 | 6 | import { HostTree, SchematicContext } from '@angular-devkit/schematics'; |
4 | 7 | import { minVersion as semverMinVersion } from 'semver'; |
5 | | -import { alignFirebaseVersion, firebaseVersionRange, pinInstalledPrereleaseVersion } from './common.js'; |
| 8 | +import { |
| 9 | + alignFirebaseVersion, |
| 10 | + firebase13VersionRange, |
| 11 | + firebaseVersionRange, |
| 12 | + pinInstalledPrereleaseVersion, |
| 13 | + supportedFirebaseRange, |
| 14 | +} from './common.js'; |
6 | 15 | import 'jasmine'; |
7 | 16 |
|
8 | 17 | const context = { logger: new logging.Logger('test') } as unknown as SchematicContext; |
@@ -135,6 +144,11 @@ const treeWithFirebase = (firebaseVersion?: string, section = 'dependencies') => |
135 | 144 | return tree; |
136 | 145 | }; |
137 | 146 |
|
| 147 | +const withInstalledFirebase = (tree: HostTree, version: string) => { |
| 148 | + tree.create('node_modules/firebase/package.json', JSON.stringify({ name: 'firebase', version })); |
| 149 | + return tree; |
| 150 | +}; |
| 151 | + |
138 | 152 | describe('alignFirebaseVersion', () => { |
139 | 153 |
|
140 | 154 | it('rewrites a previous-major range', () => { |
@@ -214,9 +228,116 @@ describe('alignFirebaseVersion', () => { |
214 | 228 | expect(sectionIn(tree, 'devDependencies').firebase).toBe(firebaseVersionRange); |
215 | 229 | }); |
216 | 230 |
|
| 231 | + it('leaves a firebase 13 range untouched', () => { |
| 232 | + for (const declared of ['^13.0.0', '^13.1.0', '~13.1.0', supportedFirebaseRange]) { |
| 233 | + const tree = treeWithFirebase(declared); |
| 234 | + expect(alignFirebaseVersion(tree, context)).withContext(declared).toBeFalse(); |
| 235 | + expect(dependenciesIn(tree).firebase).withContext(declared).toBe(declared); |
| 236 | + } |
| 237 | + }); |
| 238 | + |
| 239 | + it('rewrites an out-of-range entry that starts at firebase 13 to the 13 range, not back to 12', () => { |
| 240 | + for (const declared of ['>=13.0.0', '^13.0.0-20260929163754']) { |
| 241 | + const tree = treeWithFirebase(declared); |
| 242 | + expect(alignFirebaseVersion(tree, context)).withContext(declared).toBeTrue(); |
| 243 | + expect(dependenciesIn(tree).firebase).withContext(declared).toBe(firebase13VersionRange); |
| 244 | + } |
| 245 | + }); |
| 246 | + |
| 247 | + it('rewrites an out-of-range entry that reaches below firebase 13 to the 12 range when nothing is installed', () => { |
| 248 | + for (const declared of ['*', '>=12.18.0']) { |
| 249 | + const tree = treeWithFirebase(declared); |
| 250 | + expect(alignFirebaseVersion(tree, context)).withContext(declared).toBeTrue(); |
| 251 | + expect(dependenciesIn(tree).firebase).withContext(declared).toBe(firebaseVersionRange); |
| 252 | + } |
| 253 | + }); |
| 254 | + |
| 255 | + it('adds the range of the firebase major already installed', () => { |
| 256 | + for (const [installed, expected] of [['13.1.0', firebase13VersionRange], ['12.19.0', firebaseVersionRange]]) { |
| 257 | + const tree = withInstalledFirebase(treeWithFirebase(undefined), installed); |
| 258 | + expect(alignFirebaseVersion(tree, context)).withContext(installed).toBeTrue(); |
| 259 | + expect(dependenciesIn(tree).firebase).withContext(installed).toBe(expected); |
| 260 | + } |
| 261 | + }); |
| 262 | + |
| 263 | + it('rewrites an out-of-range entry to the range of the firebase major already installed', () => { |
| 264 | + for (const [installed, expected] of [['13.1.0', firebase13VersionRange], ['12.19.0', firebaseVersionRange]]) { |
| 265 | + const tree = withInstalledFirebase(treeWithFirebase('*'), installed); |
| 266 | + expect(alignFirebaseVersion(tree, context)).withContext(installed).toBeTrue(); |
| 267 | + expect(dependenciesIn(tree).firebase).withContext(installed).toBe(expected); |
| 268 | + } |
| 269 | + }); |
| 270 | + |
| 271 | + it('falls back to the declared range when the installed firebase is outside both majors', () => { |
| 272 | + for (const [installed, declared, expected] of [ |
| 273 | + ['11.10.0', '^11.0.0', firebaseVersionRange], |
| 274 | + ['13.0.0-20260929163754', '^13.0.0-20260929163754', firebase13VersionRange], |
| 275 | + ]) { |
| 276 | + const tree = withInstalledFirebase(treeWithFirebase(declared), installed); |
| 277 | + expect(alignFirebaseVersion(tree, context)).withContext(installed).toBeTrue(); |
| 278 | + expect(dependenciesIn(tree).firebase).withContext(installed).toBe(expected); |
| 279 | + } |
| 280 | + }); |
| 281 | + |
| 282 | + it('ignores an installed copy that the workspace entry excludes', () => { |
| 283 | + for (const [installed, declared, expected] of [ |
| 284 | + ['13.1.0', '~12.10.0', firebaseVersionRange], |
| 285 | + ['12.19.0', '>=13.0.0', firebase13VersionRange], |
| 286 | + ]) { |
| 287 | + const tree = withInstalledFirebase(treeWithFirebase(declared), installed); |
| 288 | + expect(alignFirebaseVersion(tree, context)).withContext(declared).toBeTrue(); |
| 289 | + expect(dependenciesIn(tree).firebase).withContext(declared).toBe(expected); |
| 290 | + } |
| 291 | + }); |
| 292 | + |
| 293 | + it('finds firebase installed at a monorepo root above the workspace', () => { |
| 294 | + const repository = mkdtempSync(join(tmpdir(), 'angularfire-monorepo-')); |
| 295 | + try { |
| 296 | + const workspace = join(repository, 'apps', 'web'); |
| 297 | + mkdirSync(workspace, { recursive: true }); |
| 298 | + mkdirSync(join(repository, 'node_modules', 'firebase'), { recursive: true }); |
| 299 | + writeFileSync(join(repository, 'node_modules', 'firebase', 'package.json'), |
| 300 | + JSON.stringify({ name: 'firebase', version: '13.1.0', exports: { './package.json': './package.json' } })); |
| 301 | + writeFileSync(join(workspace, 'package.json'), JSON.stringify({ name: 'web' })); |
| 302 | + // The host `ng add` and `ng update` give a schematic: the real filesystem, scoped to the workspace. |
| 303 | + const tree = new HostTree(new virtualFs.ScopedHost(new NodeJsSyncHost(), normalize(workspace))); |
| 304 | + expect(alignFirebaseVersion(tree, context)).toBeTrue(); |
| 305 | + expect(dependenciesIn(tree).firebase).toBe(firebase13VersionRange); |
| 306 | + } finally { |
| 307 | + // rmdirSync, not rmSync: this repo's @types/node predates fs.rmSync. |
| 308 | + rmdirSync(repository, { recursive: true }); |
| 309 | + } |
| 310 | + }); |
| 311 | + |
| 312 | + it('ignores an installed firebase manifest it cannot read', () => { |
| 313 | + const tree = treeWithFirebase(undefined); |
| 314 | + tree.create('node_modules/firebase/package.json', 'not json'); |
| 315 | + expect(alignFirebaseVersion(tree, context)).toBeTrue(); |
| 316 | + expect(dependenciesIn(tree).firebase).toBe(firebaseVersionRange); |
| 317 | + }); |
| 318 | + |
| 319 | + it('mentions the supported range when it rewrites an entry', () => { |
| 320 | + const infoSpy = spyOn(context.logger, 'info'); |
| 321 | + alignFirebaseVersion(treeWithFirebase('^11.0.0'), context); |
| 322 | + expect(infoSpy).toHaveBeenCalledWith(jasmine.stringContaining(supportedFirebaseRange)); |
| 323 | + }); |
| 324 | + |
217 | 325 | it('matches the firebase range the library declares as its peer dependency', () => { |
218 | 326 | const libraryManifest = JSON.parse(readFileSync('src/package.json', 'utf8')); |
219 | | - expect(libraryManifest.peerDependencies.firebase).toBe(firebaseVersionRange); |
| 327 | + expect(libraryManifest.peerDependencies.firebase).toBe(supportedFirebaseRange); |
| 328 | + }); |
| 329 | + |
| 330 | + it('builds against the oldest firebase major the peer dependency accepts', () => { |
| 331 | + /* tools/build.ts imports wrapped functions by name from the installed firebase, so building |
| 332 | + * on a newer major can emit an import that the older major does not export. */ |
| 333 | + const rootManifest = JSON.parse(readFileSync('package.json', 'utf8')); |
| 334 | + expect(semverMinVersion(rootManifest.dependencies.firebase)?.major) |
| 335 | + .toBe(semverMinVersion(supportedFirebaseRange)?.major); |
| 336 | + }); |
| 337 | + |
| 338 | + it('writes a range for exactly the majors the peer dependency accepts', () => { |
| 339 | + expect(supportedFirebaseRange.split('||').map(range => range.trim()).sort()) |
| 340 | + .toEqual([firebaseVersionRange, firebase13VersionRange].sort()); |
220 | 341 | }); |
221 | 342 |
|
222 | 343 | it('is a no-op when run a second time', () => { |
|
0 commit comments