diff --git a/lib/manager/cargo/artifacts.spec.ts b/lib/manager/cargo/artifacts.spec.ts index aa867d7a8729570dcd7584012b3f40053df160e4..fcffac55b09d06a5d93b8a1c7ace970dd57130f5 100644 --- a/lib/manager/cargo/artifacts.spec.ts +++ b/lib/manager/cargo/artifacts.spec.ts @@ -1,24 +1,17 @@ -import { exec as _exec } from 'child_process'; -import _fs from 'fs-extra'; import { join } from 'upath'; -import { envMock, mockExecAll } from '../../../test/exec-util'; -import { git, mocked } from '../../../test/util'; +import { envMock, exec, mockExecAll } from '../../../test/exec-util'; +import { env, fs, git } from '../../../test/util'; import { GlobalConfig } from '../../config/global'; import type { RepoGlobalConfig } from '../../config/types'; import * as docker from '../../util/exec/docker'; -import * as _env from '../../util/exec/env'; import type { UpdateArtifactsConfig } from '../types'; import * as cargo from './artifacts'; -jest.mock('fs-extra'); jest.mock('child_process'); jest.mock('../../util/exec/env'); jest.mock('../../util/git'); jest.mock('../../util/http'); - -const fs: jest.Mocked<typeof _fs> = _fs as any; -const exec: jest.Mock<typeof _exec> = _exec as any; -const env = mocked(_env); +jest.mock('../../util/fs'); const config: UpdateArtifactsConfig = {}; @@ -39,6 +32,7 @@ describe('manager/cargo/artifacts', () => { afterEach(() => { GlobalConfig.reset(); }); + it('returns null if no Cargo.lock found', async () => { fs.stat.mockRejectedValue(new Error('not found!')); const updatedDeps = [ @@ -55,6 +49,7 @@ describe('manager/cargo/artifacts', () => { }) ).toBeNull(); }); + it('returns null if updatedDeps is empty', async () => { expect( await cargo.updateArtifacts({ @@ -65,11 +60,14 @@ describe('manager/cargo/artifacts', () => { }) ).toBeNull(); }); + it('returns null if unchanged', async () => { fs.stat.mockResolvedValueOnce({ name: 'Cargo.lock' } as any); - fs.readFile.mockResolvedValueOnce('Current Cargo.lock' as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('Current Cargo.lock'); const execSnapshots = mockExecAll(exec); - fs.readFile.mockResolvedValueOnce('Current Cargo.lock' as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('Current Cargo.lock'); const updatedDeps = [ { @@ -86,11 +84,14 @@ describe('manager/cargo/artifacts', () => { ).toBeNull(); expect(execSnapshots).toMatchSnapshot(); }); + it('returns updated Cargo.lock', async () => { fs.stat.mockResolvedValueOnce({ name: 'Cargo.lock' } as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); git.getFile.mockResolvedValueOnce('Old Cargo.lock'); const execSnapshots = mockExecAll(exec); - fs.readFile.mockResolvedValueOnce('New Cargo.lock' as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock'); const updatedDeps = [ { depName: 'dep1', @@ -111,7 +112,8 @@ describe('manager/cargo/artifacts', () => { fs.stat.mockResolvedValueOnce({ name: 'Cargo.lock' } as any); git.getFile.mockResolvedValueOnce('Old Cargo.lock'); const execSnapshots = mockExecAll(exec); - fs.readFile.mockResolvedValueOnce('New Cargo.lock' as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock'); const updatedDeps = [ { depName: 'renamedDep1', @@ -136,7 +138,8 @@ describe('manager/cargo/artifacts', () => { git.getFile.mockResolvedValueOnce('Old Cargo.lock'); const execSnapshots = mockExecAll(exec); - fs.readFile.mockResolvedValueOnce('New Cargo.lock' as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock'); const updatedDeps = [ { depName: 'dep1', @@ -157,7 +160,8 @@ describe('manager/cargo/artifacts', () => { fs.stat.mockResolvedValueOnce({ name: 'Cargo.lock' } as any); git.getFile.mockResolvedValueOnce('Old Cargo.lock'); const execSnapshots = mockExecAll(exec); - fs.readFile.mockResolvedValueOnce('New Cargo.lock' as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock'); expect( await cargo.updateArtifacts({ packageFileName: 'Cargo.toml', @@ -174,7 +178,8 @@ describe('manager/cargo/artifacts', () => { GlobalConfig.set({ ...adminConfig, binarySource: 'docker' }); git.getFile.mockResolvedValueOnce('Old Cargo.lock'); const execSnapshots = mockExecAll(exec); - fs.readFile.mockResolvedValueOnce('New Cargo.lock' as any); + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock'); const updatedDeps = [ { depName: 'dep1', @@ -192,8 +197,9 @@ describe('manager/cargo/artifacts', () => { }); it('catches errors', async () => { fs.stat.mockResolvedValueOnce({ name: 'Cargo.lock' } as any); - fs.readFile.mockResolvedValueOnce('Current Cargo.lock' as any); - fs.outputFile.mockImplementationOnce(() => { + fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock'); + fs.readLocalFile.mockResolvedValueOnce('Current Cargo.lock'); + fs.writeLocalFile.mockImplementationOnce(() => { throw new Error('not found'); }); const updatedDeps = [