Skip to content

Commit

Permalink
test(manager/cargo): migrate to fs.readLocalFile (#13063)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
olegkrivtsov and viceice committed Dec 13, 2021
1 parent a02f390 commit 93e088f
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions 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 = {};

Expand All @@ -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 = [
Expand All @@ -55,6 +49,7 @@ describe('manager/cargo/artifacts', () => {
})
).toBeNull();
});

it('returns null if updatedDeps is empty', async () => {
expect(
await cargo.updateArtifacts({
Expand All @@ -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 = [
{
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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 = [
Expand Down

0 comments on commit 93e088f

Please sign in to comment.