|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import * as snapshot from 'snap-shot-it'; |
| 16 | +import {describe, it} from 'mocha'; |
| 17 | +import {expect} from 'chai'; |
| 18 | +import {RootComposerUpdatePackages} from '../../src/updaters/php/root-composer-update-packages'; |
| 19 | +import {Version, VersionsMap} from '../../src/version'; |
| 20 | + |
| 21 | +describe('PHPComposer', () => { |
| 22 | + describe('updateContent', () => { |
| 23 | + it('does not update a version when version is the same', async () => { |
| 24 | + const oldContent = '{"version":"1.0.0","replace":{"version":"1.0.0"}}'; |
| 25 | + |
| 26 | + const version = Version.parse('1.0.0'); |
| 27 | + |
| 28 | + const versionsMap: VersionsMap = new Map(); |
| 29 | + |
| 30 | + const newContent = new RootComposerUpdatePackages({ |
| 31 | + version, |
| 32 | + versionsMap, |
| 33 | + }).updateContent(oldContent); |
| 34 | + |
| 35 | + expect(newContent).to.eq( |
| 36 | + '{"version":"1.0.0","replace":{"version":"1.0.0"}}' |
| 37 | + ); |
| 38 | + |
| 39 | + snapshot(newContent); |
| 40 | + }); |
| 41 | + |
| 42 | + it('update all versions in composer.json', async () => { |
| 43 | + const oldContent = '{"version":"0.0.0","replace":{"version":"0.0.0"}}'; |
| 44 | + |
| 45 | + const version = Version.parse('1.0.0'); |
| 46 | + |
| 47 | + const versionsMap: VersionsMap = new Map(); |
| 48 | + |
| 49 | + versionsMap.set('version', version); |
| 50 | + |
| 51 | + const newContent = new RootComposerUpdatePackages({ |
| 52 | + version, |
| 53 | + versionsMap, |
| 54 | + }).updateContent(oldContent); |
| 55 | + |
| 56 | + expect(newContent).to.eq( |
| 57 | + '{"version":"1.0.0","replace":{"version":"1.0.0"}}' |
| 58 | + ); |
| 59 | + |
| 60 | + snapshot(newContent); |
| 61 | + }); |
| 62 | + |
| 63 | + it('update root version in composer.json', async () => { |
| 64 | + const oldContent = '{"version":"0.0.0"}'; |
| 65 | + |
| 66 | + const version = Version.parse('1.0.0'); |
| 67 | + |
| 68 | + const versionsMap: VersionsMap = new Map(); |
| 69 | + |
| 70 | + versionsMap.set('version', version); |
| 71 | + |
| 72 | + const newContent = new RootComposerUpdatePackages({ |
| 73 | + version, |
| 74 | + versionsMap, |
| 75 | + }).updateContent(oldContent); |
| 76 | + |
| 77 | + expect(newContent).to.eq('{"version":"1.0.0"}'); |
| 78 | + |
| 79 | + snapshot(newContent); |
| 80 | + }); |
| 81 | + |
| 82 | + it('update replace version in composer.json when version is present', async () => { |
| 83 | + const oldContent = '{"replace":{"version":"0.0.0"}}'; |
| 84 | + |
| 85 | + const version = Version.parse('1.0.0'); |
| 86 | + |
| 87 | + const versionsMap: VersionsMap = new Map(); |
| 88 | + |
| 89 | + versionsMap.set('version', version); |
| 90 | + |
| 91 | + const newContent = new RootComposerUpdatePackages({ |
| 92 | + version, |
| 93 | + versionsMap, |
| 94 | + }).updateContent(oldContent); |
| 95 | + |
| 96 | + expect(newContent).to.eq('{"replace":{"version":"1.0.0"}}'); |
| 97 | + |
| 98 | + snapshot(newContent); |
| 99 | + }); |
| 100 | + |
| 101 | + it('update replace version in composer.json when version is missing', async () => { |
| 102 | + const oldContent = '{"replace":{}}'; |
| 103 | + |
| 104 | + const version = Version.parse('1.0.0'); |
| 105 | + |
| 106 | + const versionsMap: VersionsMap = new Map(); |
| 107 | + |
| 108 | + versionsMap.set('version', version); |
| 109 | + |
| 110 | + const newContent = new RootComposerUpdatePackages({ |
| 111 | + version, |
| 112 | + versionsMap, |
| 113 | + }).updateContent(oldContent); |
| 114 | + |
| 115 | + expect(newContent).to.eq('{"replace":{"version":"1.0.0"}}'); |
| 116 | + |
| 117 | + snapshot(newContent); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments