From c4e10c23224072eecae642c05f830ace6923516c Mon Sep 17 00:00:00 2001 From: Matt Travi Date: Mon, 22 Jul 2024 23:51:39 -0500 Subject: [PATCH] feat(config): migrate legacy config name to modern config name --- README.md | 2 +- example.js | 2 +- src/config/index.js | 1 + src/config/lifter.js | 8 +++++ src/config/lifter.test.js | 35 +++++++++++++++++++ src/lift/lift.js | 5 ++- src/lift/lift.test.js | 9 +++-- test/integration/features/lift/config.feature | 6 ++++ .../features/step_definitions/config-steps.js | 13 ++++++- 9 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 src/config/lifter.js create mode 100644 src/config/lifter.test.js create mode 100644 test/integration/features/lift/config.feature diff --git a/README.md b/README.md index 65f16ef9..2bfa63a4 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ import {lift, test as projectUsesRenovate, scaffold} from '@form8ion/renovate-sc await scaffold({projectRoot: process.cwd()}); if (await projectUsesRenovate({projectRoot: process.cwd()})) { - await lift(); + await lift({projectRoot: process.cwd()}); } })(); ``` diff --git a/example.js b/example.js index a4b5e328..644cf66f 100644 --- a/example.js +++ b/example.js @@ -11,6 +11,6 @@ stubbedFs(); await scaffold({projectRoot: process.cwd()}); if (await projectUsesRenovate({projectRoot: process.cwd()})) { - await lift(); + await lift({projectRoot: process.cwd()}); } })(); diff --git a/src/config/index.js b/src/config/index.js index 8a5a8f5a..3ebb2e96 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -1 +1,2 @@ export {default as scaffold} from './scaffolder.js'; +export {default as lift} from './lifter.js'; diff --git a/src/config/lifter.js b/src/config/lifter.js new file mode 100644 index 00000000..a8693aba --- /dev/null +++ b/src/config/lifter.js @@ -0,0 +1,8 @@ +import {promises as fs} from 'node:fs'; +import {fileExists} from '@form8ion/core'; + +export default async function ({projectRoot}) { + if (await fileExists(`${projectRoot}/renovate.json`)) { + await fs.rename(`${projectRoot}/renovate.json`, `${projectRoot}/.renovaterc.json`); + } +} diff --git a/src/config/lifter.test.js b/src/config/lifter.test.js new file mode 100644 index 00000000..110b325d --- /dev/null +++ b/src/config/lifter.test.js @@ -0,0 +1,35 @@ +import {promises as fs} from 'node:fs'; +import {fileExists} from '@form8ion/core'; + +import any from '@travi/any'; +import {when} from 'jest-when'; +import {afterEach, describe, expect, it, vi} from 'vitest'; + +import liftConfig from './lifter.js'; + +vi.mock('node:fs'); +vi.mock('@form8ion/core'); + +describe('config lifter', () => { + const projectRoot = any.string(); + + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should move legacy config to the modern filename', async () => { + when(fileExists).calledWith(`${projectRoot}/renovate.json`).mockResolvedValue(true); + + await liftConfig({projectRoot}); + + expect(fs.rename).toHaveBeenCalledWith(`${projectRoot}/renovate.json`, `${projectRoot}/.renovaterc.json`); + }); + + it('should not attempt to move legacy config if none exists', async () => { + when(fileExists).calledWith(`${projectRoot}/renovate.json`).mockResolvedValue(false); + + await liftConfig({projectRoot}); + + expect(fs.rename).not.toHaveBeenCalled(); + }); +}); diff --git a/src/lift/lift.js b/src/lift/lift.js index ab3d6dcb..3b66331c 100644 --- a/src/lift/lift.js +++ b/src/lift/lift.js @@ -1,8 +1,11 @@ import deepmerge from 'deepmerge'; import {scaffold as scaffoldBadges} from '../badges/index.js'; +import {lift as liftConfig} from '../config/index.js'; + +export default async function ({projectRoot}) { + await liftConfig({projectRoot}); -export default function () { return deepmerge.all([ scaffoldBadges(), {branchesToVerify: ['renovate/**']} diff --git a/src/lift/lift.test.js b/src/lift/lift.test.js index 63d6a079..a352eef1 100644 --- a/src/lift/lift.test.js +++ b/src/lift/lift.test.js @@ -5,24 +5,29 @@ import {when} from 'jest-when'; import any from '@travi/any'; import {scaffold as scaffoldBadges} from '../badges/index.js'; +import {lift as liftConfig} from '../config/index.js'; import lift from './lift.js'; vi.mock('deepmerge'); vi.mock('../badges'); +vi.mock('../config/index.js'); describe('lift', () => { afterEach(() => { vi.clearAllMocks(); }); - it('should define renovate branches as branches to verify', () => { + it('should define renovate branches as branches to verify', async () => { const badgesResults = any.simpleObject(); const mergedResults = any.simpleObject(); + const projectRoot = any.string(); scaffoldBadges.mockReturnValue(badgesResults); when(deepmerge.all) .calledWith([badgesResults, {branchesToVerify: ['renovate/**']}]) .mockReturnValue(mergedResults); - expect(lift()).toEqual(mergedResults); + expect(await lift({projectRoot})).toEqual(mergedResults); + + expect(liftConfig).toHaveBeenCalledWith({projectRoot}); }); }); diff --git a/test/integration/features/lift/config.feature b/test/integration/features/lift/config.feature new file mode 100644 index 00000000..694c4853 --- /dev/null +++ b/test/integration/features/lift/config.feature @@ -0,0 +1,6 @@ +Feature: Lift Config + + Scenario: legacy config + Given the project uses a renovate config with the legacy filename + When the scaffolder results are processed + Then the project uses config with the modern filename diff --git a/test/integration/features/step_definitions/config-steps.js b/test/integration/features/step_definitions/config-steps.js index ea4c5e14..53f0b4f7 100644 --- a/test/integration/features/step_definitions/config-steps.js +++ b/test/integration/features/step_definitions/config-steps.js @@ -1,8 +1,19 @@ import {promises as fs} from 'node:fs'; +import {fileExists} from '@form8ion/core'; -import {Given} from '@cucumber/cucumber'; +import assert from 'node:assert'; +import {Given, Then} from '@cucumber/cucumber'; import any from '@travi/any'; Given('the project uses a renovate config with the modern filename', async function () { await fs.writeFile(`${this.projectRoot}/.renovaterc.json`, JSON.stringify(any.simpleObject())); }); + +Given('the project uses a renovate config with the legacy filename', async function () { + await fs.writeFile(`${this.projectRoot}/renovate.json`, JSON.stringify(any.simpleObject())); +}); + +Then('the project uses config with the modern filename', async function () { + assert.equal(await fileExists(`${this.projectRoot}/renovate.json`), false); + assert.equal(await fileExists(`${this.projectRoot}/.renovaterc.json`), true); +});