Skip to content

Commit

Permalink
test: remove rimraf and fs-extra dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-agius4 committed Jun 7, 2021
1 parent 2d0d82b commit 2f03e7a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 160 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@
"@types/postcss-preset-env": "^6.7.1",
"@types/progress": "^2.0.3",
"@types/resolve": "^1.17.1",
"@types/rimraf": "^3.0.0",
"@types/sass": "^1.16.0",
"@types/semver": "^7.0.0",
"@types/text-table": "^0.2.1",
Expand Down Expand Up @@ -204,7 +203,6 @@
"raw-loader": "4.0.2",
"regenerator-runtime": "0.13.7",
"resolve-url-loader": "4.0.0",
"rimraf": "3.0.2",
"rxjs": "6.6.7",
"sass": "1.34.1",
"sass-loader": "12.0.0",
Expand Down
222 changes: 78 additions & 144 deletions tests/legacy-cli/e2e/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -1,195 +1,127 @@
import * as fs from 'fs-extra';
import {dirname} from 'path';
import {stripIndents} from 'common-tags';


export function readFile(fileName: string) {
return new Promise<string>((resolve, reject) => {
fs.readFile(fileName, 'utf-8', (err: any, data: string) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}
import { promises as fs, constants } from 'fs';
import { dirname, join } from 'path';
import { stripIndents } from 'common-tags';

export function writeFile(fileName: string, content: string, options?: any) {
return new Promise<void>((resolve, reject) => {
fs.writeFile(fileName, content, options, (err: any) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
export function readFile(fileName: string): Promise<string> {
return fs.readFile(fileName, 'utf-8');
}


export function deleteFile(path: string) {
return new Promise<void>((resolve, reject) => {
fs.unlink(path, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
export function writeFile(fileName: string, content: string, options?: any): Promise<void> {
return fs.writeFile(fileName, content, options);
}


export function rimraf(path: string) {
return new Promise<void>((resolve, reject) => {
fs.remove(path, (err?: any) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
export function deleteFile(path: string): Promise<void> {
return fs.unlink(path);
}


export function moveFile(from: string, to: string) {
return new Promise<void>((resolve, reject) => {
fs.rename(from, to, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
export function rimraf(path: string): Promise<void> {
return fs.rmdir(path, { recursive: true, maxRetries: 3 });
}


export function symlinkFile(from: string, to: string, type?: string) {
return new Promise<void>((resolve, reject) => {
fs.symlink(from, to, type, (err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
export function moveFile(from: string, to: string): Promise<void> {
return fs.rename(from, to);
}

export function createDir(path: string) {
return _recursiveMkDir(path);
export function symlinkFile(from: string, to: string, type?: string): Promise<void> {
return fs.symlink(from, to, type);
}


function _recursiveMkDir(path: string): Promise<void> {
if (fs.existsSync(path)) {
return Promise.resolve();
} else {
return _recursiveMkDir(dirname(path))
.then(() => fs.mkdirSync(path));
}
export function createDir(path: string): Promise<void> {
return fs.mkdir(path, { recursive: true });
}

export function copyFile(from: string, to: string) {
return _recursiveMkDir(dirname(to))
.then(() => new Promise((resolve, reject) => {
const rd = fs.createReadStream(from);
rd.on('error', (err: Error) => reject(err));
export async function copyFile(from: string, to: string): Promise<void> {
await createDir(dirname(to));

const wr = fs.createWriteStream(to);
wr.on('error', (err: Error) => reject(err));
wr.on('close', () => resolve());

rd.pipe(wr);
}));
return fs.copyFile(from, to, constants.COPYFILE_FICLONE);
}

export function moveDirectory(from: string, to: string) {
return fs.move(from, to, { overwrite: true });
}
export async function moveDirectory(from: string, to: string): Promise<void> {
await rimraf(to);
await createDir(to);

for (const entry of await fs.readdir(from)) {
const fromEntry = join(from, entry);
const toEntry = join(to, entry);
if ((await fs.stat(fromEntry)).isFile()) {
await copyFile(fromEntry, toEntry);
} else {
await moveDirectory(fromEntry, toEntry);
}
}
}

export function writeMultipleFiles(fs: { [path: string]: string }) {
return Promise.all(Object.keys(fs).map(fileName => writeFile(fileName, fs[fileName])));
return Promise.all(Object.keys(fs).map((fileName) => writeFile(fileName, fs[fileName])));
}


export function replaceInFile(filePath: string, match: RegExp | string, replacement: string) {
return readFile(filePath)
.then((content: string) => writeFile(filePath, content.replace(match, replacement)));
return readFile(filePath).then((content: string) =>
writeFile(filePath, content.replace(match, replacement)),
);
}


export function appendToFile(filePath: string, text: string, options?: any) {
return readFile(filePath)
.then((content: string) => writeFile(filePath, content.concat(text), options));
return readFile(filePath).then((content: string) =>
writeFile(filePath, content.concat(text), options),
);
}


export function prependToFile(filePath: string, text: string, options?: any) {
return readFile(filePath)
.then((content: string) => writeFile(filePath, text.concat(content), options));
return readFile(filePath).then((content: string) =>
writeFile(filePath, text.concat(content), options),
);
}

export async function expectFileMatchToExist(dir: string, regex: RegExp): Promise<string> {
const files = await fs.readdir(dir);
const fileName = files.find((name) => regex.test(name));

export function expectFileMatchToExist(dir: string, regex: RegExp) {
return new Promise((resolve, reject) => {
const [fileName] = fs.readdirSync(dir).filter(name => name.match(regex));
if (!fileName) {
reject(new Error(`File ${regex} was expected to exist but not found...`));
}
resolve(fileName);
});
if (!fileName) {
throw new Error(`File ${regex} was expected to exist but not found...`);
}

return fileName;
}

export function expectFileNotToExist(fileName: string) {
return new Promise((resolve, reject) => {
fs.exists(fileName, (exist) => {
if (exist) {
reject(new Error(`File ${fileName} was expected not to exist but found...`));
} else {
resolve();
}
});
});
export async function expectFileNotToExist(fileName: string): Promise<void> {
try {
await fs.access(fileName, constants.F_OK);
} catch {
return;
}

throw new Error(`File ${fileName} was expected not to exist but found...`);
}

export function expectFileToExist(fileName: string) {
return new Promise((resolve, reject) => {
fs.exists(fileName, (exist) => {
if (exist) {
resolve();
} else {
reject(new Error(`File ${fileName} was expected to exist but not found...`));
}
});
});
export async function expectFileToExist(fileName: string): Promise<void> {
try {
await fs.access(fileName, constants.F_OK);
} catch {
throw new Error(`File ${fileName} was expected to exist but not found...`);
}
}

export function expectFileToMatch(fileName: string, regEx: RegExp | string) {
return readFile(fileName)
.then(content => {
if (typeof regEx == 'string') {
if (content.indexOf(regEx) == -1) {
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
return readFile(fileName).then((content) => {
if (typeof regEx == 'string') {
if (content.indexOf(regEx) == -1) {
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
Content:
${content}
------
`);
}
} else {
if (!content.match(regEx)) {
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
}
} else {
if (!content.match(regEx)) {
throw new Error(stripIndents`File "${fileName}" did not contain "${regEx}"...
Content:
${content}
------
`);
}
}
});
}
});
}

export async function getFileSize(fileName: string) {
Expand All @@ -202,6 +134,8 @@ export async function expectFileSizeToBeUnder(fileName: string, sizeInBytes: num
const fileSize = await getFileSize(fileName);

if (fileSize > sizeInBytes) {
throw new Error(`File "${fileName}" exceeded file size of "${sizeInBytes}". Size is ${fileSize}.`);
throw new Error(
`File "${fileName}" exceeded file size of "${sizeInBytes}". Size is ${fileSize}.`,
);
}
}
18 changes: 4 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@

"@angular/dev-infra-private@https://github.com/angular/dev-infra-private-builds.git#7308e71916da94b51a9b558693c49a04bfc98beb":
version "0.0.0"
uid "7308e71916da94b51a9b558693c49a04bfc98beb"
resolved "https://github.com/angular/dev-infra-private-builds.git#7308e71916da94b51a9b558693c49a04bfc98beb"
resolved "https://github.com/angular/dev-infra-private-builds.git#ccf49db26d09093bdb3882282d99f3448cf68cba"
dependencies:
"@angular/benchpress" "0.2.1"
"@bazel/buildifier" "^4.0.1"
Expand All @@ -113,9 +112,9 @@
semver "^7.3.5"
shelljs "^0.8.4"
ts-node "^10.0.0"
tslib "^2.2.0"
tslib "^2.1.0"
typed-graphqlify "^3.1.1"
typescript "~4.3.2"
typescript "~4.2.4"
yaml "^1.10.0"
yargs "^17.0.0"

Expand Down Expand Up @@ -1670,7 +1669,7 @@
resolved "https://registry.yarnpkg.com/@types/find-cache-dir/-/find-cache-dir-3.2.0.tgz#eaaf331699dccf52c47926e4d4f8f3ed8db33f3c"
integrity sha512-+JeT9qb2Jwzw72WdjU+TSvD5O1QRPWCeRpDJV+guiIq+2hwR0DFGw+nZNbTFjMIVe6Bf4GgAKeB/6Ytx6+MbeQ==

"@types/glob@*", "@types/glob@^7.1.1":
"@types/glob@^7.1.1":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
integrity sha512-SEYeGAIQIQX8NN6LDKprLjbrd5dARM5EXsd8GI/A5l0apYI1fGMWgPHSe4ZKL4eozlAyI+doUE9XbYS4xCkQ1w==
Expand Down Expand Up @@ -1878,14 +1877,6 @@
dependencies:
"@types/node" "*"

"@types/rimraf@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@types/rimraf/-/rimraf-3.0.0.tgz#b9d03f090ece263671898d57bb7bb007023ac19f"
integrity sha512-7WhJ0MdpFgYQPXlF4Dx+DhgvlPCfz/x5mHaeDQAKhcenvQP1KCpLQ18JklAqeGMYSAT2PxLpzd0g2/HE7fj7hQ==
dependencies:
"@types/glob" "*"
"@types/node" "*"

"@types/sass@^1.16.0":
version "1.16.0"
resolved "https://registry.yarnpkg.com/@types/sass/-/sass-1.16.0.tgz#b41ac1c17fa68ffb57d43e2360486ef526b3d57d"
Expand Down Expand Up @@ -10213,7 +10204,6 @@ [email protected], sass@^1.32.8:

"sauce-connect-proxy@https://saucelabs.com/downloads/sc-4.6.4-linux.tar.gz":
version "0.0.0"
uid "992e2cb0d91e54b27a4f5bbd2049f3b774718115"
resolved "https://saucelabs.com/downloads/sc-4.6.4-linux.tar.gz#992e2cb0d91e54b27a4f5bbd2049f3b774718115"

saucelabs@^1.5.0:
Expand Down

0 comments on commit 2f03e7a

Please sign in to comment.