Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GITHUB-6525-5172): Rewrite copyDirContentsSyncAllow to call fs-extra::copySync() on the directories instead of calling it on the files to copy individually #6526

Merged
merged 1 commit into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/classes/Utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ describe('Utils', () => {
});

describe('#copyDirContentsSync()', () => {
it('recursively copy directory files', () => {
it('should recursively copy directory files', () => {
const tmpSrcDirPath = path.join(process.cwd(), 'testSrc');
const tmpDestDirPath = path.join(process.cwd(), 'testDest');

Expand Down
18 changes: 9 additions & 9 deletions lib/utils/fs/copyDirContentsSync.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict';

const path = require('path');
const fs = require('fs');
const fse = require('./fse');
const walkDirSync = require('./walkDirSync');

function fileExists(srcDir, destDir, options) {
const fullFilesPaths = walkDirSync(srcDir, options);
const isNotSymbolicLink = src => !fs.lstatSync(src).isSymbolicLink();

fullFilesPaths.forEach(fullFilePath => {
const relativeFilePath = fullFilePath.replace(srcDir, '');
fse.copySync(fullFilePath, path.join(destDir, relativeFilePath));
});
function copyDirContentsSync(srcDir, destDir, { noLinks = false } = {}) {
const copySyncOptions = {
dereference: true,
filter: noLinks ? isNotSymbolicLink : null,
};
fse.copySync(srcDir, destDir, copySyncOptions);
}

module.exports = fileExists;
module.exports = copyDirContentsSync;
75 changes: 75 additions & 0 deletions lib/utils/fs/copyDirContentsSync.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
'use strict';

const expect = require('chai').expect;
const fs = require('fs');
const path = require('path');
const copyDirContentsSync = require('./copyDirContentsSync');
const fileExistsSync = require('./fileExistsSync');
const removeFileSync = require('./removeFileSync');
const writeFileSync = require('./writeFileSync');
const { skipOnWindowsDisabledSymlinks } = require('../../../tests/utils/misc');

describe('#copyDirContentsSync()', () => {
afterEach(() => {
removeFileSync(path.join(process.cwd(), 'testSrc'));
removeFileSync(path.join(process.cwd(), 'testDest'));
});

it('should recursively copy directory files including symbolic links', function() {
const tmpSrcDirPath = path.join(process.cwd(), 'testSrc');
const tmpDestDirPath = path.join(process.cwd(), 'testDest');

const srcFile1 = path.join(tmpSrcDirPath, 'file1.txt');
const srcFile2 = path.join(tmpSrcDirPath, 'folder', 'file2.txt');
const srcFile3 = path.join(tmpSrcDirPath, 'folder', 'file3.txt');

const destFile1 = path.join(tmpDestDirPath, 'file1.txt');
const destFile2 = path.join(tmpDestDirPath, 'folder', 'file2.txt');
const destFile3 = path.join(tmpDestDirPath, 'folder', 'file3.txt');

writeFileSync(srcFile1, 'foo');
writeFileSync(srcFile2, 'bar');
try {
fs.symlinkSync(srcFile2, srcFile3);
} catch (error) {
skipOnWindowsDisabledSymlinks(error, this);
throw error;
}

copyDirContentsSync(tmpSrcDirPath, tmpDestDirPath);

expect(fileExistsSync(destFile1)).to.equal(true);
expect(fileExistsSync(destFile2)).to.equal(true);
expect(fileExistsSync(destFile3)).to.equal(true);
});

it('should recursively copy directory files excluding symbolic links', function() {
const tmpSrcDirPath = path.join(process.cwd(), 'testSrc');
const tmpDestDirPath = path.join(process.cwd(), 'testDest');

const srcFile1 = path.join(tmpSrcDirPath, 'file1.txt');
const srcFile2 = path.join(tmpSrcDirPath, 'folder', 'file2.txt');
const srcFile3 = path.join(tmpSrcDirPath, 'folder', 'file3.txt');

const destFile1 = path.join(tmpDestDirPath, 'file1.txt');
const destFile2 = path.join(tmpDestDirPath, 'folder', 'file2.txt');
const destFile3 = path.join(tmpDestDirPath, 'folder', 'file3.txt');

writeFileSync(srcFile1, 'foo');
writeFileSync(srcFile2, 'bar');
try {
fs.symlinkSync(srcFile2, srcFile3);
} catch (error) {
skipOnWindowsDisabledSymlinks(error, this);
throw error;
}

copyDirContentsSync(tmpSrcDirPath, tmpDestDirPath, {
noLinks: true,
});

expect(fileExistsSync(destFile1)).to.equal(true);
expect(fileExistsSync(destFile2)).to.equal(true);
expect(fileExistsSync(destFile3)).to.equal(false);
});
});