Skip to content

Commit

Permalink
test: updateYarnBinary()
Browse files Browse the repository at this point in the history
  • Loading branch information
ylemkimon committed Oct 28, 2021
1 parent d829b4a commit 6de8930
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 3 deletions.
49 changes: 49 additions & 0 deletions lib/manager/npm/post-update/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`manager/npm/post-update/index updateYarnBinary() should return .yarnrc.yml content if it has been overwritten 1`] = `
"yarnPath: .yarn/releases/yarn-3.0.2.cjs
a: b
"
`;

exports[`manager/npm/post-update/index updateYarnBinary() should return .yarnrc.yml content if it has been overwritten 2`] = `
Array [
Object {
"contents": "yarnPath: .yarn/releases/yarn-3.0.2.cjs
a: b
",
"name": "path/to/lockfile/.yarnrc.yml",
},
Object {
"contents": "path/to/lockfile/.yarn/releases/yarn-3.0.1.cjs",
"name": "|delete|",
},
Object {
"contents": "new yarn
",
"executable": true,
"name": "path/to/lockfile/.yarn/releases/yarn-3.0.2.cjs",
},
]
`;

exports[`manager/npm/post-update/index updateYarnBinary() should update the Yarn binary 1`] = `
Array [
Object {
"contents": "yarnPath: .yarn/releases/yarn-3.0.2.cjs
a: b
",
"name": "path/to/lockfile/.yarnrc.yml",
},
Object {
"contents": "path/to/lockfile/.yarn/releases/yarn-3.0.1.cjs",
"name": "|delete|",
},
Object {
"contents": "new yarn
",
"executable": true,
"name": "path/to/lockfile/.yarn/releases/yarn-3.0.2.cjs",
},
]
`;
88 changes: 88 additions & 0 deletions lib/manager/npm/post-update/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// TODO: add tests
import { fs, git } from '../../../../test/util';
import { updateYarnBinary } from './index';

jest.mock('../../../util/fs');
jest.mock('../../../util/git');

describe('manager/npm/post-update/index', () => {
const lockFileDir = `path/to/lockfile`;
const oldYarnrcYml = `yarnPath: .yarn/releases/yarn-3.0.1.cjs\na: b\n`;
const newYarnrcYml = `yarnPath: .yarn/releases/yarn-3.0.2.cjs\nc: d\n`;
const newYarn = `new yarn\n`;

beforeEach(() => {
git.getFile = jest.fn();
fs.readLocalFile = jest.fn();
});

describe('updateYarnBinary()', () => {
it('should update the Yarn binary', async () => {
git.getFile.mockResolvedValueOnce(oldYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(newYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(newYarn);
const updatedArtifacts = [];
const yarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
undefined
);
expect(yarnrcYmlContent).toBeUndefined();
expect(updatedArtifacts).toMatchSnapshot();
});

it('should return .yarnrc.yml content if it has been overwritten', async () => {
fs.readLocalFile.mockResolvedValueOnce(newYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(newYarn);
let existingYarnrcYmlContent = oldYarnrcYml;
const updatedArtifacts = [];
existingYarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
existingYarnrcYmlContent
);
expect(git.getFile).not.toHaveBeenCalled();
expect(existingYarnrcYmlContent).toMatchSnapshot();
expect(updatedArtifacts).toMatchSnapshot();
});

it("should not update the Yarn binary if the old .yarnrc.yml doesn't exist", async () => {
git.getFile.mockResolvedValueOnce(null);
fs.readLocalFile.mockResolvedValueOnce(newYarnrcYml);
const updatedArtifacts = [];
const yarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
undefined
);
expect(yarnrcYmlContent).toBeUndefined();
expect(updatedArtifacts).toBeEmpty();
});

it("should not update the Yarn binary if the new .yarnrc.yml doesn't exist", async () => {
git.getFile.mockResolvedValueOnce(oldYarnrcYml);
fs.readLocalFile.mockResolvedValueOnce(null);
const updatedArtifacts = [];
const yarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
undefined
);
expect(yarnrcYmlContent).toBeUndefined();
expect(updatedArtifacts).toBeEmpty();
});

it("should return existing .yarnrc.yml if the new one doesn't exist", async () => {
fs.readLocalFile.mockResolvedValueOnce(null);
let existingYarnrcYmlContent = oldYarnrcYml;
const updatedArtifacts = [];
existingYarnrcYmlContent = await updateYarnBinary(
lockFileDir,
updatedArtifacts,
existingYarnrcYmlContent
);
expect(existingYarnrcYmlContent).toMatch(existingYarnrcYmlContent);
expect(updatedArtifacts).toBeEmpty();
});
});
});
6 changes: 3 additions & 3 deletions lib/manager/npm/post-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ async function updateYarnOffline(
}
}

// istanbul ignore next
async function updateYarnBinary(
// exported for testing
export async function updateYarnBinary(
lockFileDir: string,
updatedArtifacts: UpdatedArtifacts[],
existingYarnrcYmlContent: string | undefined
Expand Down Expand Up @@ -410,7 +410,7 @@ async function updateYarnBinary(
executable: true,
}
);
} catch (err) {
} catch (err) /* istanbul ignore next */ {
logger.error({ err }, 'Error updating Yarn binary');
}
return existingYarnrcYmlContent && yarnrcYml;
Expand Down

0 comments on commit 6de8930

Please sign in to comment.