From 82ee689f710a8ce5c9366a4e3554dc5011123f30 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sun, 7 May 2023 07:57:14 +0200 Subject: [PATCH 1/8] feat: platform=local --- lib/config/presets/local/index.ts | 1 + lib/constants/platforms.ts | 3 +- lib/modules/platform/api.ts | 2 + lib/modules/platform/local/index.ts | 123 ++++++++++++++++++++++++ lib/modules/platform/local/scm.ts | 45 +++++++++ lib/modules/platform/scm.ts | 2 + lib/util/fs/index.spec.ts | 5 + lib/util/fs/index.ts | 4 + lib/util/git/index.ts | 4 + lib/workers/global/autodiscover.ts | 9 ++ lib/workers/global/index.ts | 11 ++- lib/workers/repository/process/index.ts | 2 +- package.json | 2 +- 13 files changed, 206 insertions(+), 7 deletions(-) create mode 100644 lib/modules/platform/local/index.ts create mode 100644 lib/modules/platform/local/scm.ts diff --git a/lib/config/presets/local/index.ts b/lib/config/presets/local/index.ts index ac8e2a69703049..6f99390a6c676d 100644 --- a/lib/config/presets/local/index.ts +++ b/lib/config/presets/local/index.ts @@ -24,6 +24,7 @@ const resolvers = { gitea, github, gitlab, + local: null, } satisfies Record; export function getPreset({ diff --git a/lib/constants/platforms.ts b/lib/constants/platforms.ts index f3a1e71ac8b7ba..049155bd3ba43c 100644 --- a/lib/constants/platforms.ts +++ b/lib/constants/platforms.ts @@ -5,7 +5,8 @@ export type PlatformId = | 'bitbucket-server' | 'gitea' | 'github' - | 'gitlab'; + | 'gitlab' + | 'local'; export const GITHUB_API_USING_HOST_TYPES = [ 'github', diff --git a/lib/modules/platform/api.ts b/lib/modules/platform/api.ts index bb67f2c39432a4..973f6b75d39db6 100644 --- a/lib/modules/platform/api.ts +++ b/lib/modules/platform/api.ts @@ -6,6 +6,7 @@ import * as codecommit from './codecommit'; import * as gitea from './gitea'; import * as github from './github'; import * as gitlab from './gitlab'; +import * as local from './local'; import type { Platform } from './types'; const api = new Map(); @@ -18,3 +19,4 @@ api.set(codecommit.id, codecommit); api.set(gitea.id, gitea); api.set(github.id, github); api.set(gitlab.id, gitlab); +api.set(local.id, local); diff --git a/lib/modules/platform/local/index.ts b/lib/modules/platform/local/index.ts new file mode 100644 index 00000000000000..1c88d787371daf --- /dev/null +++ b/lib/modules/platform/local/index.ts @@ -0,0 +1,123 @@ +import type { BranchStatus } from '../../../types'; +import type { + Issue, + PlatformParams, + PlatformResult, + Pr, + RepoResult, +} from '../types'; + +export const id = 'local'; + +export function initPlatform(params: PlatformParams): Promise { + return Promise.resolve({ + dryRun: 'lookup', + endpoint: 'local', + persistRepoData: true, + requireConfig: 'optional', + }); +} + +export function getRepos(): Promise { + return Promise.resolve([]); +} + +export function initRepo(): Promise { + return Promise.resolve({ + defaultBranch: '', + isFork: false, + repoFingerprint: '', + }); +} + +export function getRepoForceRebase(): Promise { + return Promise.resolve(false); +} + +export function findIssue(): Promise { + return Promise.resolve(null); +} + +export function getIssueList(): Promise { + return Promise.resolve([]); +} + +export function getRawFile(): Promise { + return Promise.resolve(null); +} + +export function getJsonFile(): Promise | null> { + return Promise.resolve(null); +} + +export function getPrList(): Promise { + return Promise.resolve([]); +} + +export function ensureIssueClosing(): Promise { + return Promise.resolve(); +} + +export function ensureIssue(): Promise { + return Promise.resolve(null); +} + +export function massageMarkdown(input: string): string { + return input; +} + +export function updatePr(): Promise { + return Promise.resolve(); +} + +export function mergePr(): Promise { + return Promise.resolve(false); +} + +export function addReviewers(): Promise { + return Promise.resolve(); +} + +export function addAssignees(): Promise { + return Promise.resolve(); +} + +export function createPr(): Promise { + return Promise.resolve(null); +} + +export function deleteLabel(): Promise { + return Promise.resolve(); +} + +export function setBranchStatus(): Promise { + return Promise.resolve(); +} + +export function getBranchStatus(): Promise { + return Promise.resolve('red'); +} + +export function getBranchStatusCheck(): Promise { + return Promise.resolve(null); +} + +export function ensureCommentRemoval(): Promise { + return Promise.resolve(); +} + +export function ensureComment(): Promise { + return Promise.resolve(false); +} + +export function getPr(): Promise { + return Promise.resolve(null); +} + +export function findPr(): Promise { + return Promise.resolve(null); +} + +export function getBranchPr(): Promise { + return Promise.resolve(null); +} diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts new file mode 100644 index 00000000000000..61a8f57204c3b2 --- /dev/null +++ b/lib/modules/platform/local/scm.ts @@ -0,0 +1,45 @@ +import { glob } from 'glob'; +import type { CommitFilesConfig, CommitSha } from '../../../util/git/types'; +import type { PlatformScm } from '../types'; + +let fileList: string[] | undefined; +export class LocalFs implements PlatformScm { + fileList: string[] | undefined; + + isBranchBehindBase(branchName: string, baseBranch: string): Promise { + return Promise.resolve(false); + } + isBranchModified(branchName: string): Promise { + return Promise.resolve(false); + } + isBranchConflicted(baseBranch: string, branch: string): Promise { + return Promise.resolve(false); + } + branchExists(branchName: string): Promise { + return Promise.resolve(true); + } + getBranchCommit(branchName: string): Promise { + return Promise.resolve(null); + } + deleteBranch(branchName: string): Promise { + return Promise.resolve(); + } + commitAndPush(commitConfig: CommitFilesConfig): Promise { + return Promise.resolve(null); + } + + async getFileList(): Promise { + fileList ??= await glob('**', { + dot: true, + nodir: true, + ignore: { + childrenIgnored: (p) => p.isNamed('.git') || p.isNamed('node_modules'), + }, + }); + return fileList; + } + + checkoutBranch(branchName: string): Promise { + return Promise.resolve(''); + } +} diff --git a/lib/modules/platform/scm.ts b/lib/modules/platform/scm.ts index 9a26a35594401d..8f80217ec36da5 100644 --- a/lib/modules/platform/scm.ts +++ b/lib/modules/platform/scm.ts @@ -3,6 +3,7 @@ import type { PlatformId } from '../../constants'; import { PLATFORM_NOT_FOUND } from '../../constants/error-messages'; import { DefaultGitScm } from './default-scm'; import { GithubScm } from './github/scm'; +import { LocalFs } from './local/scm'; import type { PlatformScm } from './types'; export const platformScmImpls = new Map>(); @@ -13,6 +14,7 @@ platformScmImpls.set('bitbucket-server', DefaultGitScm); platformScmImpls.set('gitea', DefaultGitScm); platformScmImpls.set('github', GithubScm); platformScmImpls.set('gitlab', DefaultGitScm); +platformScmImpls.set('local', LocalFs); let _scm: PlatformScm | undefined; diff --git a/lib/util/fs/index.spec.ts b/lib/util/fs/index.spec.ts index f200818843e2d7..361f4eff62dda9 100644 --- a/lib/util/fs/index.spec.ts +++ b/lib/util/fs/index.spec.ts @@ -134,6 +134,11 @@ describe('util/fs/index', () => { }); describe('deleteLocalFile', () => { + it('throws if platform is local', async () => { + GlobalConfig.set({ platform: 'local' }); + await expect(deleteLocalFile('foo/bar/file.txt')).rejects.toThrow(); + }); + it('deletes file', async () => { const filePath = `${localDir}/foo/bar/file.txt`; await fs.outputFile(filePath, 'foobar'); diff --git a/lib/util/fs/index.ts b/lib/util/fs/index.ts index 5493a6397f8493..2a9b2793975755 100644 --- a/lib/util/fs/index.ts +++ b/lib/util/fs/index.ts @@ -65,6 +65,10 @@ export async function writeLocalFile( } export async function deleteLocalFile(fileName: string): Promise { + // This a failsafe and hopefully will never be triggered + if (GlobalConfig.get('platform') === 'local') { + throw new Error('Cannot delete file when platform=local'); + } const localDir = GlobalConfig.get('localDir'); if (localDir) { const localFileName = ensureLocalPath(fileName); diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 806bda394b9889..59cbe973a184e0 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -379,6 +379,10 @@ export async function syncGit(): Promise { if (gitInitialized) { return; } + // istanbul ignore if: failsafe + if (GlobalConfig.get('platform') === 'local') { + throw new Error('Cannot sync git when platform=local'); + } gitInitialized = true; const localDir = GlobalConfig.get('localDir')!; logger.debug(`Initializing git repository into ${localDir}`); diff --git a/lib/workers/global/autodiscover.ts b/lib/workers/global/autodiscover.ts index 332961b82e1506..03045a5aca1658 100644 --- a/lib/workers/global/autodiscover.ts +++ b/lib/workers/global/autodiscover.ts @@ -13,6 +13,15 @@ function repoName(value: string | { repository: string }): string { export async function autodiscoverRepositories( config: AllConfig ): Promise { + if (config.platform === 'local') { + if (config.repositories?.length) { + logger.warn( + { repositories: config.repositories }, + 'platform=local supports only cwd' + ); + } + config.repositories = ['local']; + } if (!config.autodiscover) { if (!config.repositories?.length) { logger.warn( diff --git a/lib/workers/global/index.ts b/lib/workers/global/index.ts index 52e7beb5ea9a67..80869a7426c87e 100644 --- a/lib/workers/global/index.ts +++ b/lib/workers/global/index.ts @@ -37,10 +37,13 @@ export async function getRepositoryConfig( ); // TODO: types (#7154) const platform = GlobalConfig.get('platform')!; - repoConfig.localDir = upath.join( - repoConfig.baseDir, - `./repos/${platform}/${repoConfig.repository}` - ); + repoConfig.localDir = + platform === 'local' + ? process.cwd() + : upath.join( + repoConfig.baseDir, + `./repos/${platform}/${repoConfig.repository}` + ); await fs.ensureDir(repoConfig.localDir); delete repoConfig.baseDir; return configParser.filterConfig(repoConfig, 'repository'); diff --git a/lib/workers/repository/process/index.ts b/lib/workers/repository/process/index.ts index 0593f539bc42bb..cced4e1702d921 100644 --- a/lib/workers/repository/process/index.ts +++ b/lib/workers/repository/process/index.ts @@ -108,7 +108,7 @@ export async function extractDependencies( branchList: [], packageFiles: null!, }; - if (config.baseBranches?.length) { + if (GlobalConfig.get('platform') !== 'local' && config.baseBranches?.length) { config.baseBranches = unfoldBaseBranches(config.baseBranches); logger.debug({ baseBranches: config.baseBranches }, 'baseBranches'); const extracted: Record> = {}; diff --git a/package.json b/package.json index 59258853236b25..c7d0588b389c47 100644 --- a/package.json +++ b/package.json @@ -196,6 +196,7 @@ "global-agent": "3.0.0", "good-enough-parser": "1.1.22", "got": "11.8.6", + "glob": "10.2.2", "graph-data-structure": "3.3.0", "handlebars": "4.7.7", "hasha": "5.2.2", @@ -307,7 +308,6 @@ "eslint-plugin-promise": "6.1.1", "eslint-plugin-typescript-enum": "2.1.0", "expect-more-jest": "5.5.0", - "glob": "10.2.2", "graphql": "16.6.0", "husky": "8.0.3", "jest": "29.5.0", From 9c009355db9cf44d9e129034c2aa94eb0d34dfee Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sun, 7 May 2023 17:52:47 +0200 Subject: [PATCH 2/8] tests --- lib/modules/platform/local/index.ts | 1 + lib/modules/platform/local/scm.ts | 1 + lib/workers/global/autodiscover.spec.ts | 6 ++++++ lib/workers/global/autodiscover.ts | 7 +++++-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/modules/platform/local/index.ts b/lib/modules/platform/local/index.ts index 1c88d787371daf..5dd98d30a1a287 100644 --- a/lib/modules/platform/local/index.ts +++ b/lib/modules/platform/local/index.ts @@ -1,3 +1,4 @@ +/* istanbul ignore file */ import type { BranchStatus } from '../../../types'; import type { Issue, diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts index 61a8f57204c3b2..e1f67e585c1e1d 100644 --- a/lib/modules/platform/local/scm.ts +++ b/lib/modules/platform/local/scm.ts @@ -1,3 +1,4 @@ +/* istanbul ignore file */ import { glob } from 'glob'; import type { CommitFilesConfig, CommitSha } from '../../../util/git/types'; import type { PlatformScm } from '../types'; diff --git a/lib/workers/global/autodiscover.spec.ts b/lib/workers/global/autodiscover.spec.ts index e401268d6fd148..5e38dd1603f816 100644 --- a/lib/workers/global/autodiscover.spec.ts +++ b/lib/workers/global/autodiscover.spec.ts @@ -25,6 +25,12 @@ describe('workers/global/autodiscover', () => { }); }); + it('throws if local and repositories defined', async () => { + config.platform = 'local'; + config.repositories = ['a']; + await expect(autodiscoverRepositories(config)).rejects.toThrow(); + }); + it('returns if not autodiscovering', async () => { expect(await autodiscoverRepositories(config)).toEqual(config); }); diff --git a/lib/workers/global/autodiscover.ts b/lib/workers/global/autodiscover.ts index 03045a5aca1658..e2908104a8d521 100644 --- a/lib/workers/global/autodiscover.ts +++ b/lib/workers/global/autodiscover.ts @@ -15,9 +15,12 @@ export async function autodiscoverRepositories( ): Promise { if (config.platform === 'local') { if (config.repositories?.length) { - logger.warn( + logger.debug( { repositories: config.repositories }, - 'platform=local supports only cwd' + 'Found repositories when in local mode' + ); + throw new Error( + 'Invalid configuration: repositories list not supported when platform=local' ); } config.repositories = ['local']; From ee2c6254d3ee168ee5447aceebdf6ca035e33c43 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sun, 7 May 2023 18:14:00 +0200 Subject: [PATCH 3/8] Update lib/modules/platform/local/scm.ts --- lib/modules/platform/local/scm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts index e1f67e585c1e1d..05e4807df15367 100644 --- a/lib/modules/platform/local/scm.ts +++ b/lib/modules/platform/local/scm.ts @@ -34,7 +34,7 @@ export class LocalFs implements PlatformScm { dot: true, nodir: true, ignore: { - childrenIgnored: (p) => p.isNamed('.git') || p.isNamed('node_modules'), + childrenIgnored: (p) => p.isNamed('.git'), }, }); return fileList; From 54f6ee6f68bbd263e7eafa68d92974acf48d0d0d Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Mon, 8 May 2023 07:13:52 +0200 Subject: [PATCH 4/8] coverage --- lib/modules/platform/local/index.spec.ts | 128 +++++++++++++++++++++++ lib/modules/platform/local/index.ts | 1 - lib/modules/platform/local/scm.ts | 1 + lib/workers/global/autodiscover.spec.ts | 7 ++ lib/workers/global/autodiscover.ts | 1 + lib/workers/global/index.spec.ts | 9 ++ 6 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 lib/modules/platform/local/index.spec.ts diff --git a/lib/modules/platform/local/index.spec.ts b/lib/modules/platform/local/index.spec.ts new file mode 100644 index 00000000000000..c226d334d79cbe --- /dev/null +++ b/lib/modules/platform/local/index.spec.ts @@ -0,0 +1,128 @@ +import * as platform from './index'; + +describe('modules/platform/local/index', () => { + describe('initPlatform', () => { + it('returns input', async () => { + expect(await platform.initPlatform({})).toMatchInlineSnapshot(` + { + "dryRun": "lookup", + "endpoint": "local", + "persistRepoData": true, + "requireConfig": "optional", + } + `); + }); + }); + + describe('getRepos', () => { + it('returns empty array', async () => { + expect(await platform.getRepos()).toEqual([]); + }); + }); + + describe('initRepo', () => { + it('returns object', async () => { + expect(await platform.initRepo()).toMatchInlineSnapshot(` + { + "defaultBranch": "", + "isFork": false, + "repoFingerprint": "", + } + `); + }); + }); + + describe('dummy functions', () => { + it('getRepoForceRebase', async () => { + expect(await platform.getRepoForceRebase()).toBe(false); + }); + + it('findIssue', async () => { + expect(await platform.findIssue()).toBeNull(); + }); + + it('getIssueList', async () => { + expect(await platform.getIssueList()).toEqual([]); + }); + + it('getRawFile', async () => { + expect(await platform.getRawFile()).toBeNull(); + }); + + it('getJsonFile', async () => { + expect(await platform.getJsonFile()).toBeNull(); + }); + + it('getPrList', async () => { + expect(await platform.getPrList()).toEqual([]); + }); + + it('ensureIssueClosing', async () => { + expect(await platform.ensureIssueClosing()).toBeUndefined(); + }); + + it('ensureIssue', async () => { + expect(await platform.ensureIssue()).toBeNull(); + }); + + it('massageMarkdown', () => { + expect(platform.massageMarkdown('foo')).toBe('foo'); + }); + + it('updatePr', async () => { + expect(await platform.updatePr()).toBeUndefined(); + }); + + it('mergePr', async () => { + expect(await platform.mergePr()).toBe(false); + }); + + it('addReviewers', async () => { + expect(await platform.addReviewers()).toBeUndefined(); + }); + + it('addAssignees', async () => { + expect(await platform.addAssignees()).toBeUndefined(); + }); + + it('createPr', async () => { + expect(await platform.createPr()).toBeNull(); + }); + + it('deleteLabel', async () => { + expect(await platform.deleteLabel()).toBeUndefined(); + }); + + it('setBranchStatus', async () => { + expect(await platform.setBranchStatus()).toBeUndefined(); + }); + + it('getBranchStatus', async () => { + expect(await platform.getBranchStatus()).toBe('red'); + }); + + it('getBranchStatusCheck', async () => { + expect(await platform.getBranchStatusCheck()).toBeNull(); + }); + + it('ensureCommentRemoval', async () => { + expect(await platform.ensureCommentRemoval()).toBeUndefined(); + }); + + it('ensureComment', async () => { + expect(await platform.ensureComment()).toBeFalse(); + }); + + it('getPr', async () => { + expect(await platform.getPr()).toBeNull(); + }); + + it('findPr', async () => { + expect(await platform.findPr()).toBeNull(); + }); + + it('getBranchPr', async () => { + expect(await platform.getBranchPr()).toBeNull(); + }); + }); +}); diff --git a/lib/modules/platform/local/index.ts b/lib/modules/platform/local/index.ts index 5dd98d30a1a287..1c88d787371daf 100644 --- a/lib/modules/platform/local/index.ts +++ b/lib/modules/platform/local/index.ts @@ -1,4 +1,3 @@ -/* istanbul ignore file */ import type { BranchStatus } from '../../../types'; import type { Issue, diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts index 05e4807df15367..f2b96a1c29bf9b 100644 --- a/lib/modules/platform/local/scm.ts +++ b/lib/modules/platform/local/scm.ts @@ -1,4 +1,5 @@ /* istanbul ignore file */ + import { glob } from 'glob'; import type { CommitFilesConfig, CommitSha } from '../../../util/git/types'; import type { PlatformScm } from '../types'; diff --git a/lib/workers/global/autodiscover.spec.ts b/lib/workers/global/autodiscover.spec.ts index 5e38dd1603f816..ae75305ad695a9 100644 --- a/lib/workers/global/autodiscover.spec.ts +++ b/lib/workers/global/autodiscover.spec.ts @@ -31,6 +31,13 @@ describe('workers/global/autodiscover', () => { await expect(autodiscoverRepositories(config)).rejects.toThrow(); }); + it('returns local', async () => { + config.platform = 'local'; + expect((await autodiscoverRepositories(config)).repositories).toEqual([ + 'local', + ]); + }); + it('returns if not autodiscovering', async () => { expect(await autodiscoverRepositories(config)).toEqual(config); }); diff --git a/lib/workers/global/autodiscover.ts b/lib/workers/global/autodiscover.ts index e2908104a8d521..943f739d67f247 100644 --- a/lib/workers/global/autodiscover.ts +++ b/lib/workers/global/autodiscover.ts @@ -24,6 +24,7 @@ export async function autodiscoverRepositories( ); } config.repositories = ['local']; + return config; } if (!config.autodiscover) { if (!config.repositories?.length) { diff --git a/lib/workers/global/index.spec.ts b/lib/workers/global/index.spec.ts index 8c371725b1569a..0bc6aec1add7c0 100644 --- a/lib/workers/global/index.spec.ts +++ b/lib/workers/global/index.spec.ts @@ -95,6 +95,15 @@ describe('workers/global/index', () => { expect(repositoryWorker.renovateRepository).not.toHaveBeenCalled(); }); + it('handles local', async () => { + parseConfigs.mockResolvedValueOnce({ + platform: 'local', + }); + await expect(globalWorker.start()).resolves.toBe(0); + expect(parseConfigs).toHaveBeenCalledTimes(1); + expect(repositoryWorker.renovateRepository).toHaveBeenCalledTimes(1); + }); + it('processes repositories', async () => { parseConfigs.mockResolvedValueOnce({ gitAuthor: 'a@b.com', From 95f1dbd5be9dc14dd8de9321c0a51b61e37e2513 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Mon, 8 May 2023 13:41:45 +0200 Subject: [PATCH 5/8] Use git for getFileList --- lib/modules/platform/local/scm.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts index f2b96a1c29bf9b..c27aa632bd5a0a 100644 --- a/lib/modules/platform/local/scm.ts +++ b/lib/modules/platform/local/scm.ts @@ -1,9 +1,14 @@ /* istanbul ignore file */ +import { exec } from 'child_process'; +import { promisify } from 'util'; import { glob } from 'glob'; +import { logger } from '../../../logger'; import type { CommitFilesConfig, CommitSha } from '../../../util/git/types'; import type { PlatformScm } from '../types'; +const execAsync = promisify(exec); + let fileList: string[] | undefined; export class LocalFs implements PlatformScm { fileList: string[] | undefined; @@ -31,13 +36,22 @@ export class LocalFs implements PlatformScm { } async getFileList(): Promise { - fileList ??= await glob('**', { - dot: true, - nodir: true, - ignore: { - childrenIgnored: (p) => p.isNamed('.git'), - }, - }); + try { + // fetch file list using git + const { stdout } = await execAsync('git ls-files'); + logger.debug('Got file list using git'); + fileList = stdout.split('\n'); + } catch (err) { + logger.debug('Could not get file list using git, using glob instead'); + fileList ??= await glob('**', { + dot: true, + nodir: true, + ignore: { + childrenIgnored: (p) => p.isNamed('.git'), + }, + }); + } + return fileList; } From 11dfe6fa1d07828ae5a01827d8968a80a48eef0f Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Tue, 9 May 2023 06:47:09 +0200 Subject: [PATCH 6/8] add test --- lib/modules/platform/local/scm.spec.ts | 68 ++++++++++++++++++++++++++ lib/modules/platform/local/scm.ts | 12 +---- 2 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 lib/modules/platform/local/scm.spec.ts diff --git a/lib/modules/platform/local/scm.spec.ts b/lib/modules/platform/local/scm.spec.ts new file mode 100644 index 00000000000000..6549475ad55d93 --- /dev/null +++ b/lib/modules/platform/local/scm.spec.ts @@ -0,0 +1,68 @@ +import { execSync as _execSync } from 'node:child_process'; +import { mockedFunction } from '../../../../test/util'; +import { LocalFs } from './scm'; + +jest.mock('node:child_process'); +const execSync = mockedFunction(_execSync); + +describe('modules/platform/local/scm', () => { + let localFs: LocalFs; + + beforeEach(() => { + localFs = new LocalFs(); + }); + + describe('dummy functions', () => { + it('behindBaseBranch', async () => { + expect(await localFs.isBranchBehindBase('', '')).toBe(false); + }); + + it('isBranchModified', async () => { + expect(await localFs.isBranchModified('')).toBe(false); + }); + + it('isBranchConflicted', async () => { + expect(await localFs.isBranchConflicted('', '')).toBe(false); + }); + + it('branchExists', async () => { + expect(await localFs.branchExists('')).toBe(true); + }); + + it('getBranchCommit', async () => { + expect(await localFs.getBranchCommit('')).toBeNull(); + }); + + it('deleteBranch', async () => { + expect(await localFs.deleteBranch('')).toBeUndefined(); + }); + + it('commitAndPush', async () => { + expect(await localFs.commitAndPush({} as any)).toBeNull(); + }); + + it('checkoutBranch', async () => { + expect(await localFs.checkoutBranch('')).toBe(''); + }); + }); + + describe('getFileList', () => { + it('should return file list using git', async () => { + execSync.mockReturnValueOnce('file1\nfile2'); + expect(await localFs.getFileList()).toHaveLength(2); + }); + + it('should return file list using glob', async () => { + execSync.mockImplementationOnce(() => { + throw new Error(); + }); + jest.mock('glob', () => ({ + glob: jest + .fn() + .mockImplementation(() => Promise.resolve(['file1', 'file2'])), + })); + + expect(await localFs.getFileList()).toHaveLength(2); + }); + }); +}); diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts index c27aa632bd5a0a..6f42b8a476549e 100644 --- a/lib/modules/platform/local/scm.ts +++ b/lib/modules/platform/local/scm.ts @@ -1,14 +1,9 @@ -/* istanbul ignore file */ - -import { exec } from 'child_process'; -import { promisify } from 'util'; +import { execSync } from 'node:child_process'; import { glob } from 'glob'; import { logger } from '../../../logger'; import type { CommitFilesConfig, CommitSha } from '../../../util/git/types'; import type { PlatformScm } from '../types'; -const execAsync = promisify(exec); - let fileList: string[] | undefined; export class LocalFs implements PlatformScm { fileList: string[] | undefined; @@ -38,7 +33,7 @@ export class LocalFs implements PlatformScm { async getFileList(): Promise { try { // fetch file list using git - const { stdout } = await execAsync('git ls-files'); + const stdout = execSync('git ls-files', { encoding: 'utf-8' }); logger.debug('Got file list using git'); fileList = stdout.split('\n'); } catch (err) { @@ -46,9 +41,6 @@ export class LocalFs implements PlatformScm { fileList ??= await glob('**', { dot: true, nodir: true, - ignore: { - childrenIgnored: (p) => p.isNamed('.git'), - }, }); } From 8fc767a4c1fe89ced2bce811648dd309fd119c6b Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Tue, 9 May 2023 11:18:19 +0200 Subject: [PATCH 7/8] Update lib/modules/platform/local/scm.ts --- lib/modules/platform/local/scm.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts index 6f42b8a476549e..829be177b81684 100644 --- a/lib/modules/platform/local/scm.ts +++ b/lib/modules/platform/local/scm.ts @@ -6,7 +6,6 @@ import type { PlatformScm } from '../types'; let fileList: string[] | undefined; export class LocalFs implements PlatformScm { - fileList: string[] | undefined; isBranchBehindBase(branchName: string, baseBranch: string): Promise { return Promise.resolve(false); From 09cd5a641f3f89d95d4487b1b8d260f20cfa653d Mon Sep 17 00:00:00 2001 From: Jamie Magee Date: Tue, 9 May 2023 21:07:10 -0700 Subject: [PATCH 8/8] prettier fix --- lib/modules/platform/local/scm.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/modules/platform/local/scm.ts b/lib/modules/platform/local/scm.ts index 829be177b81684..5b01391572c110 100644 --- a/lib/modules/platform/local/scm.ts +++ b/lib/modules/platform/local/scm.ts @@ -6,7 +6,6 @@ import type { PlatformScm } from '../types'; let fileList: string[] | undefined; export class LocalFs implements PlatformScm { - isBranchBehindBase(branchName: string, baseBranch: string): Promise { return Promise.resolve(false); }