Files
react-native/scripts/__tests__/scm-utils-test.js
T
Vincenzo Vitale 10e47b891a Do not depend on an ENV variable when publishing and setting the RN version (#34746)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/34746

The changes made in
https://github.com/facebook/react-native/pull/34694
introduced the need to have the env variable TMP_PUBLISH_DIR for the publishing and set-rn-version scripts to work.
This break any usage of set-rn-version when the env variable is not set upfront.

With this change, we are creating a temp folder in the scope that requires it (e.g. set-rn-version.js) and then passing the path to the save/revert functions.

## Changelog
[Internal] [Added] - Do not depend on an ENV variable when publishing and setting the RN version.

Reviewed By: cipolleschi

Differential Revision: D39683565

fbshipit-source-id: 21d85d1c16c4cb7324636ceb5eba626ff8cbb775
2022-09-22 07:34:50 -07:00

98 lines
2.5 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
const {isTaggedLatest, saveFiles, revertFiles} = require('../scm-utils');
let execResult = null;
const cpMock = jest.fn();
const mkdirpSyncMock = jest.fn();
jest
.mock('shelljs', () => ({
exec: () => {
return {
stdout: execResult,
};
},
echo: message => {
console.log(message);
},
exit: exitCode => {
exit(exitCode);
},
cp: cpMock,
}))
.mock('fs', () => ({
existsSync: jest.fn().mockImplementation(_ => true),
}))
.mock('path', () => ({
dirname: jest
.fn()
.mockImplementation(filePath =>
filePath.includes('/') ? filePath.split('/')[0] : '.',
),
}))
.mock('mkdirp', () => ({
sync: mkdirpSyncMock,
}));
describe('scm-utils', () => {
beforeEach(() => {
jest.resetModules();
jest.resetAllMocks();
});
describe('isTaggedLatest', () => {
it('it should identify commit as tagged `latest`', () => {
execResult = '6c19dc3266b84f47a076b647a1c93b3c3b69d2c5\n';
expect(isTaggedLatest('6c19dc3266b84f47a076b647a1c93b3c3b69d2c5')).toBe(
true,
);
});
it('it should not identify commit as tagged `latest`', () => {
execResult = '6c19dc3266b84f47a076b647a1c93b3c3b69d2c5\n';
expect(isTaggedLatest('6c19dc3266b8')).toBe(false);
});
});
describe('saveFiles', () => {
it('it should save files in the temp folder', () => {
const tmpFolder = '/tmp';
saveFiles(['package.json', 'android/package.json'], tmpFolder);
expect(mkdirpSyncMock).toHaveBeenCalledWith(`${tmpFolder}/android`);
expect(cpMock).toHaveBeenNthCalledWith(
1,
'package.json',
'/tmp/package.json',
);
expect(cpMock).toHaveBeenNthCalledWith(
2,
'android/package.json',
`${tmpFolder}/android/package.json`,
);
});
});
describe('revertFiles', () => {
it('it should revert files from the temp folder', () => {
const tmpFolder = '/tmp';
revertFiles(['package.json', 'android/package.json'], tmpFolder);
expect(cpMock).toHaveBeenNthCalledWith(
1,
`${tmpFolder}/package.json`,
'package.json',
);
expect(cpMock).toHaveBeenNthCalledWith(
2,
`${tmpFolder}/android/package.json`,
'android/package.json',
);
});
});
});