|
| 1 | +// Copyright 2023 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 {readFileSync} from 'fs'; |
| 16 | +import {resolve} from 'path'; |
| 17 | +import * as snapshot from 'snap-shot-it'; |
| 18 | +import {describe, it} from 'mocha'; |
| 19 | +import {Version} from '../../src/version'; |
| 20 | +import {expect, assert} from 'chai'; |
| 21 | +import {GenericToml} from '../../src/updaters/generic-toml'; |
| 22 | + |
| 23 | +const fixturesPath = './test/updaters/fixtures'; |
| 24 | + |
| 25 | +describe('GenericToml', () => { |
| 26 | + describe('updateContent', () => { |
| 27 | + it('updates matching entry', async () => { |
| 28 | + const oldContent = readFileSync( |
| 29 | + resolve(fixturesPath, './Cargo.toml'), |
| 30 | + 'utf8' |
| 31 | + ).replace(/\r\n/g, '\n'); |
| 32 | + const updater = new GenericToml( |
| 33 | + '$.package.version', |
| 34 | + Version.parse('v2.3.4') |
| 35 | + ); |
| 36 | + const newContent = updater.updateContent(oldContent); |
| 37 | + snapshot(newContent); |
| 38 | + }); |
| 39 | + it('updates deep entry in toml', async () => { |
| 40 | + const oldContent = readFileSync( |
| 41 | + resolve(fixturesPath, './Cargo.toml'), |
| 42 | + 'utf8' |
| 43 | + ).replace(/\r\n/g, '\n'); |
| 44 | + const updater = new GenericToml( |
| 45 | + "$['dev-dependencies']..version", |
| 46 | + Version.parse('v2.3.4') |
| 47 | + ); |
| 48 | + const newContent = updater.updateContent(oldContent); |
| 49 | + snapshot(newContent); |
| 50 | + }); |
| 51 | + it('ignores non-matching entry', async () => { |
| 52 | + const oldContent = readFileSync( |
| 53 | + resolve(fixturesPath, './Cargo.toml'), |
| 54 | + 'utf8' |
| 55 | + ).replace(/\r\n/g, '\n'); |
| 56 | + const updater = new GenericToml('$.nonExistent', Version.parse('v2.3.4')); |
| 57 | + const newContent = updater.updateContent(oldContent); |
| 58 | + expect(newContent).to.eql(oldContent); |
| 59 | + }); |
| 60 | + it('warns on invalid jsonpath', async () => { |
| 61 | + const oldContent = readFileSync( |
| 62 | + resolve(fixturesPath, './Cargo.toml'), |
| 63 | + 'utf8' |
| 64 | + ).replace(/\r\n/g, '\n'); |
| 65 | + const updater = new GenericToml('bad jsonpath', Version.parse('v2.3.4')); |
| 66 | + assert.throws(() => { |
| 67 | + updater.updateContent(oldContent); |
| 68 | + }); |
| 69 | + }); |
| 70 | + it('ignores invalid file', async () => { |
| 71 | + const oldContent = readFileSync( |
| 72 | + resolve(fixturesPath, './toml/invalid.txt'), |
| 73 | + 'utf8' |
| 74 | + ).replace(/\r\n/g, '\n'); |
| 75 | + const updater = new GenericToml('$.boo', Version.parse('v2.3.4')); |
| 76 | + const newContent = updater.updateContent(oldContent); |
| 77 | + expect(newContent).to.eql(oldContent); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments