Skip to content

Commit

Permalink
Make it work on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zackradisic committed Apr 11, 2024
1 parent da35efc commit 70f8b81
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ pub const AsyncCPTaskVtable = struct {
onCopy: *const (fn (*anyopaque, [:0]const u8, [:0]const u8) void) = undefined,
onFinish: *const (fn (*anyopaque, Maybe(void)) void) = undefined,
is_shell: bool = true,
verbose: bool = false,

pub const Dead = AsyncCPTaskVtable{
.ctx = @ptrFromInt(0xDEADBEEF),
Expand Down Expand Up @@ -280,6 +281,7 @@ pub const AsyncCPTaskVtable = struct {
.onCopy = wrapOnCopy,
.onFinish = wrapOnFinish,
.ctx = ctx,
.verbose = Type.verbose,
};
}

Expand Down Expand Up @@ -328,17 +330,19 @@ pub const AsyncCpTask = struct {

pub fn onCopy(this: *AsyncCpTask, src_: anytype, dest_: anytype) void {
if (@intFromPtr(this.vtable.ctx) == @intFromPtr(AsyncCPTaskVtable.Dead.ctx)) return;
if (!this.vtable.verbose) return;
if (comptime bun.Environment.isPosix) return this.vtable.onCopy(this.vtable.ctx, src_, dest_);

var buf: bun.PathBuffer = undefined;
var buf2: bun.PathBuffer = undefined;
const src: [:0]const u8 = switch (@TypeOf(src_)) {
[:0]const u8, [:0]u8 => src_,
[:0]const u16, [:0]u16 => bun.strings.fromWPath(buf[0..], src_),
else => @compileError("Invalid type: " ++ @typeName(@TypeOf(src_))),
};
const dest: [:0]const u8 = switch (@TypeOf(dest_)) {
[:0]const u8, [:0]u8 => src_,
[:0]const u16, [:0]u16 => bun.strings.fromWPath(buf[0..], dest_),
[:0]const u16, [:0]u16 => bun.strings.fromWPath(buf2[0..], dest_),
else => @compileError("Invalid type: " ++ @typeName(@TypeOf(dest_))),
};

Expand Down
1 change: 1 addition & 0 deletions src/shell/interpreter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10576,6 +10576,7 @@ pub const Interpreter = struct {
}

const NodeCpVtable = struct {
pub const verbose = true;
inline fn shellTask(this: *NodeCpVtable) *ShellCpTask {
return @fieldParentPtr(ShellCpTask, "vtable", this);
}
Expand Down
11 changes: 7 additions & 4 deletions test/js/bun/shell/commands/cp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import fs from "fs";
import { shellInternals } from "bun:internal-for-testing";
const { builtinDisabled } = shellInternals

const p = process.platform === 'win32' ? (s: string) => s.replaceAll('/', '\\') : (s: string) => s;
// const p = (s: string) => s;

describe.if(!builtinDisabled("cp"))("bunshell cp", async () => {
TestBuilder.command`cat ${import.meta.filename} > lmao.txt; cp -v lmao.txt lmao2.txt`
.stdout("$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao2.txt\n")
.stdout(p("$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao2.txt\n"))
.ensureTempDir()
.fileEquals("lmao2.txt", await $`cat ${import.meta.filename}`.text())
.runAsTest("file -> file");

TestBuilder.command`cat ${import.meta.filename} > lmao.txt; touch lmao2.txt; cp -v lmao.txt lmao2.txt`
.stdout("$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao2.txt\n")
.stdout(p("$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao2.txt\n"))
.ensureTempDir()
.fileEquals("lmao2.txt", await $`cat ${import.meta.filename}`.text())
.runAsTest("file -> existing file replaces contents");

TestBuilder.command`cat ${import.meta.filename} > lmao.txt; mkdir lmao2; cp -v lmao.txt lmao2`
.ensureTempDir()
.stdout("$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao2/lmao.txt\n")
.stdout(p("$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao2/lmao.txt\n"))
.fileEquals("lmao2/lmao.txt", await $`cat ${import.meta.filename}`.text())
.runAsTest("file -> dir");

Expand All @@ -36,7 +39,7 @@ describe.if(!builtinDisabled("cp"))("bunshell cp", async () => {
.ensureTempDir()
.stdout(
expectSortedOutput(
"$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao3/lmao.txt\n$TEMP_DIR/lmao2.txt -> $TEMP_DIR/lmao3/lmao2.txt\n",
p("$TEMP_DIR/lmao.txt -> $TEMP_DIR/lmao3/lmao.txt\n$TEMP_DIR/lmao2.txt -> $TEMP_DIR/lmao3/lmao2.txt\n"),
),
)
.fileEquals("lmao3/lmao.txt", await $`cat ${import.meta.filename}`.text())
Expand Down

0 comments on commit 70f8b81

Please sign in to comment.