mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
The main motivation behind these changes is to rely on https://github.com/jprichardson/node-fs-extra/blob/8.1.0/docs/copy-sync.md as much as possible in order to avoid having to do the `fullFilePath.replace(srcDir, '')` operation because this operation can be error-prone. Doing so fixes the following issues because the user-submitted file paths are now correctly interpreted by fs-extra, closing both #6525 and #5172.
76 lines
2.7 KiB
JavaScript
76 lines
2.7 KiB
JavaScript
'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);
|
|
});
|
|
});
|