Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaSd committed Dec 4, 2022
1 parent 6b2a836 commit 8da9768
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 98 deletions.
166 changes: 87 additions & 79 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -752,88 +752,96 @@ async function withTempDir(action: (path: string) => Promise<void>) {
}

Deno.test("copy test", async () => {
const dir = Deno.makeTempDirSync();
const file1 = path.join(dir, "file1.txt");
const file2 = path.join(dir, "file2.txt");
Deno.writeTextFileSync(file1, "test");
await $`cp ${file1} ${file2}`;

assert($.existsSync(file1));
assert($.existsSync(file2));

const destDir = path.join(dir, "dest");
Deno.mkdirSync(destDir);
await $`cp ${file1} ${file2} ${destDir}`;

assert($.existsSync(file1));
assert($.existsSync(file2));
assert($.existsSync(rustJoin(destDir, file1)));
assert($.existsSync(rustJoin(destDir, file2)));

const newFile = path.join(dir, "new.txt");
Deno.writeTextFileSync(newFile, "test");
await $`cp ${newFile} ${destDir}`;

assert(await isDir(destDir));
assert($.existsSync(newFile));
assert($.existsSync(rustJoin(destDir, newFile)));

assertEquals(await getStdErr($`cp ${file1} ${file2} non-existant`), "cp: target 'non-existant' is not a directory\n");

assertEquals(await getStdErr($`cp "" ""`), "cp: missing file operand\n");
assertStringIncludes(await getStdErr($`cp ${file1} ""`), "cp: missing destination file operand after");

// recursive test
Deno.mkdirSync(path.join(destDir, "sub_dir"));
Deno.writeTextFileSync(path.join(destDir, "sub_dir", "sub.txt"), "test");
const destDir2 = path.join(dir, "dest2");

assertEquals(await getStdErr($`cp ${destDir} ${destDir2}`), "cp: source was a directory; maybe specify -r\n");
assert(!$.existsSync(destDir2));

await $`cp -r ${destDir} ${destDir2}`;
assert($.existsSync(destDir2));
assert($.existsSync(path.join(destDir2, "file1.txt")));
assert($.existsSync(path.join(destDir2, "file2.txt")));
assert($.existsSync(path.join(destDir2, "sub_dir", "sub.txt")));

// copy again
await $`cp -r ${destDir} ${destDir2}`;

// try copying to a file
assertStringIncludes(await getStdErr($`cp -r ${destDir} ${destDir2}/file1.txt`), "destination was a file");
await withTempDir(async (dir) => {
const file1 = path.join(dir, "file1.txt");
const file2 = path.join(dir, "file2.txt");
Deno.writeTextFileSync(file1, "test");
await $`cp ${file1} ${file2}`;

assert($.existsSync(file1));
assert($.existsSync(file2));

const destDir = path.join(dir, "dest");
Deno.mkdirSync(destDir);
await $`cp ${file1} ${file2} ${destDir}`;

assert($.existsSync(file1));
assert($.existsSync(file2));
assert($.existsSync(rustJoin(destDir, file1)));
assert($.existsSync(rustJoin(destDir, file2)));

const newFile = path.join(dir, "new.txt");
Deno.writeTextFileSync(newFile, "test");
await $`cp ${newFile} ${destDir}`;

assert(await isDir(destDir));
assert($.existsSync(newFile));
assert($.existsSync(rustJoin(destDir, newFile)));

assertEquals(
await getStdErr($`cp ${file1} ${file2} non-existent`),
"cp: target 'non-existent' is not a directory\n",
);

assertEquals(await getStdErr($`cp "" ""`), "cp: missing file operand\n");
assertStringIncludes(await getStdErr($`cp ${file1} ""`), "cp: missing destination file operand after");

// recursive test
Deno.mkdirSync(path.join(destDir, "sub_dir"));
Deno.writeTextFileSync(path.join(destDir, "sub_dir", "sub.txt"), "test");
const destDir2 = path.join(dir, "dest2");

assertEquals(await getStdErr($`cp ${destDir} ${destDir2}`), "cp: source was a directory; maybe specify -r\n");
assert(!$.existsSync(destDir2));

await $`cp -r ${destDir} ${destDir2}`;
assert($.existsSync(destDir2));
assert($.existsSync(path.join(destDir2, "file1.txt")));
assert($.existsSync(path.join(destDir2, "file2.txt")));
assert($.existsSync(path.join(destDir2, "sub_dir", "sub.txt")));

// copy again
await $`cp -r ${destDir} ${destDir2}`;

// try copying to a file
assertStringIncludes(await getStdErr($`cp -r ${destDir} ${destDir2}/file1.txt`), "destination was a file");
});
});

Deno.test("move test", async () => {
const dir = Deno.makeTempDirSync();
const file1 = path.join(dir, "file1.txt");
const file2 = path.join(dir, "file2.txt");
Deno.writeTextFileSync(file1, "test");

await $`mv ${file1} ${file2}`;
assert(!$.existsSync(file1));
assert($.existsSync(file2));

const destDir = path.join(dir, "dest");
Deno.writeTextFileSync(file1, "test"); // recreate
Deno.mkdirSync(destDir);
await $`mv ${file1} ${file2} ${destDir}`;
assert(!$.existsSync(file1));
assert(!$.existsSync(file2));
assert($.existsSync(rustJoin(destDir, file2)));
assert($.existsSync(rustJoin(destDir, file2)));

const newFile = path.join(dir, "new.txt");
Deno.writeTextFileSync(newFile, "test");
await $`mv ${newFile} ${destDir}`;
assert(await isDir(destDir));
assert(!$.existsSync(newFile));
assert($.existsSync(path.join(destDir, "new.txt")));

assertEquals(await getStdErr($`mv ${file1} ${file2} non-existant`), "mv: target 'non-existant' is not a directory\n");

assertEquals(await getStdErr($`mv "" ""`), "mv: missing operand\n");
assertStringIncludes(await getStdErr($`mv ${file1} ""`), "mv: missing destination file operand after");
await withTempDir(async (dir) => {
const file1 = path.join(dir, "file1.txt");
const file2 = path.join(dir, "file2.txt");
Deno.writeTextFileSync(file1, "test");

await $`mv ${file1} ${file2}`;
assert(!$.existsSync(file1));
assert($.existsSync(file2));

const destDir = path.join(dir, "dest");
Deno.writeTextFileSync(file1, "test"); // recreate
Deno.mkdirSync(destDir);
await $`mv ${file1} ${file2} ${destDir}`;
assert(!$.existsSync(file1));
assert(!$.existsSync(file2));
assert($.existsSync(rustJoin(destDir, file2)));
assert($.existsSync(rustJoin(destDir, file2)));

const newFile = path.join(dir, "new.txt");
Deno.writeTextFileSync(newFile, "test");
await $`mv ${newFile} ${destDir}`;
assert(await isDir(destDir));
assert(!$.existsSync(newFile));
assert($.existsSync(path.join(destDir, "new.txt")));

assertEquals(
await getStdErr($`mv ${file1} ${file2} non-existent`),
"mv: target 'non-existent' is not a directory\n",
);

assertEquals(await getStdErr($`mv "" ""`), "mv: missing operand\n");
assertStringIncludes(await getStdErr($`mv ${file1} ""`), "mv: missing destination file operand after");
});
});

async function getStdErr(cmd: CommandBuilder) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals } from "https://deno.land/std@0.130.0/testing/asserts.ts";
import { parse_arg_kinds } from "./args.ts";
import { parseArgKinds } from "./args.ts";

Deno.test("parses", () => {
const data = [
Expand All @@ -14,7 +14,7 @@ Deno.test("parses", () => {
"--test",
"-t",
];
const args = parse_arg_kinds(data);
const args = parseArgKinds(data);

assertEquals(args, [
{ arg: "f", kind: "ShortFlag" },
Expand Down
2 changes: 1 addition & 1 deletion src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export interface ArgKind {
arg: string;
}

export function parse_arg_kinds(flags: string[]): ArgKind[] {
export function parseArgKinds(flags: string[]): ArgKind[] {
const result: ArgKind[] = [];
let had_dash_dash = false;
for (const arg of flags) {
Expand Down
16 changes: 8 additions & 8 deletions src/commands/cp_mv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommandContext } from "../command_handler.ts";
import { ExecuteResult, resultFromCode } from "../result.ts";
import { isDir, isFile, isSymlink } from "../common.ts";
import { bailUnsupported, parse_arg_kinds } from "./args.ts";
import { bailUnsupported, parseArgKinds } from "./args.ts";
import { fs } from "../deps.ts";
import { resolvePath, rustJoin } from "../common.ts";

Expand All @@ -22,7 +22,7 @@ interface PathWithSpecified {
specified: string;
}

interface cpFlags {
interface CopyFlags {
recursive: boolean;
operations: { from: PathWithSpecified; to: PathWithSpecified }[];
}
Expand All @@ -34,10 +34,10 @@ async function executeCp(cwd: string, args: string[]) {
}
}

export async function parseCpArgs(cwd: string, args: string[]): Promise<cpFlags> {
export async function parseCpArgs(cwd: string, args: string[]): Promise<CopyFlags> {
const paths = [];
let recursive = false;
for (const arg of parse_arg_kinds(args)) {
for (const arg of parseArgKinds(args)) {
if (arg.kind === "Arg") paths.push(arg.arg);
else if (
(arg.arg === "recursive" && arg.kind === "LongFlag") ||
Expand All @@ -54,7 +54,7 @@ export async function parseCpArgs(cwd: string, args: string[]): Promise<cpFlags>
}

async function doCopyOperation(
flags: cpFlags,
flags: CopyFlags,
from: PathWithSpecified,
to: PathWithSpecified,
) {
Expand Down Expand Up @@ -105,7 +105,7 @@ export async function mvCommand(
}
}

interface mvFlags {
interface MoveFlags {
operations: { from: PathWithSpecified; to: PathWithSpecified }[];
}

Expand All @@ -116,10 +116,10 @@ async function executeMove(cwd: string, args: string[]) {
}
}

export async function parseMvArgs(cwd: string, args: string[]): Promise<mvFlags> {
export async function parseMvArgs(cwd: string, args: string[]): Promise<MoveFlags> {
const paths = [];

for (const arg of parse_arg_kinds(args)) {
for (const arg of parseArgKinds(args)) {
if (arg.kind === "Arg") paths.push(arg.arg);
else bailUnsupported(arg);
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/rm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommandContext } from "../command_handler.ts";
import { resolvePath } from "../common.ts";
import { ExecuteResult, resultFromCode } from "../result.ts";
import { ArgKind, parse_arg_kinds } from "./args.ts";
import { ArgKind, parseArgKinds } from "./args.ts";

export async function rmCommand(
context: CommandContext,
Expand Down Expand Up @@ -45,7 +45,7 @@ export function parseArgs(args: string[]) {
paths: [],
};

for (const arg of parse_arg_kinds(args)) {
for (const arg of parseArgKinds(args)) {
if (
(arg.arg === "recursive" && arg.kind === "LongFlag") ||
(arg.arg === "r" && arg.kind == "ShortFlag") ||
Expand Down
10 changes: 4 additions & 6 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ export function resolvePath(cwd: string, arg: string) {
}

/**
Follow rust std::path::Path::join
The advantage is it can handle joining 2 absolute paths with common part
* Follow rust std::path::Path::join
* The advantage is it can handle joining 2 absolute paths with common part
rustjoin("/a/b","/a/c") => "/a/b/c"
instead of:
Deno.path.join("/a/b","/a/c") => "/a/b/a/c"
* Rust: join("/a/b","/a/c") => "/a/b/c"
* Deno. join("/a/b","/a/c") => "/a/b/a/c"
**/
export function rustJoin(path1: string, path2: string) {
const maybeCommon = path.common([path1, path2]);
Expand Down

0 comments on commit 8da9768

Please sign in to comment.