windows: fix bun-create ci tests (#10494)

* windows: fix bun-create ci tests

* fix lint

---------

Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
Meghan Denny
2024-04-24 21:51:05 -07:00
committed by GitHub
parent ebeae0ff1c
commit 685dc02142
3 changed files with 25 additions and 5 deletions

View File

@@ -1611,6 +1611,7 @@ pub const CreateCommand = struct {
const outdir_path = filesystem.absBuf(&parts, &home_dir_buf);
home_dir_buf[outdir_path.len] = 0;
const outdir_path_ = home_dir_buf[0..outdir_path.len :0];
if (bun.path.hasAnyIllegalChars(outdir_path_)) break :outer;
std.fs.accessAbsoluteZ(outdir_path_, .{}) catch break :outer;
example_tag = Example.Tag.local_folder;
break :brk outdir_path;
@@ -1622,6 +1623,7 @@ pub const CreateCommand = struct {
const outdir_path = filesystem.absBuf(&parts, &home_dir_buf);
home_dir_buf[outdir_path.len] = 0;
const outdir_path_ = home_dir_buf[0..outdir_path.len :0];
if (bun.path.hasAnyIllegalChars(outdir_path_)) break :outer;
std.fs.accessAbsoluteZ(outdir_path_, .{}) catch break :outer;
example_tag = Example.Tag.local_folder;
break :brk outdir_path;
@@ -1633,6 +1635,7 @@ pub const CreateCommand = struct {
const outdir_path = filesystem.absBuf(&parts, &home_dir_buf);
home_dir_buf[outdir_path.len] = 0;
const outdir_path_ = home_dir_buf[0..outdir_path.len :0];
if (bun.path.hasAnyIllegalChars(outdir_path_)) break :outer;
std.fs.accessAbsoluteZ(outdir_path_, .{}) catch break :outer;
example_tag = Example.Tag.local_folder;
break :brk outdir_path;

View File

@@ -596,6 +596,24 @@ pub fn isDriveLetterT(comptime T: type, c: T) bool {
return 'a' <= c and c <= 'z' or 'A' <= c and c <= 'Z';
}
pub fn hasAnyIllegalChars(maybe_path: []const u8) bool {
if (!bun.Environment.isWindows) return false;
var maybe_path_ = maybe_path;
// check for disk discrimnator; remove it since it has a ':'
if (startsWithDiskDiscriminator(maybe_path_)) maybe_path_ = maybe_path_[2..];
// guard against OBJECT_NAME_INVALID => unreachable
return bun.strings.indexAnyComptime(maybe_path_, "<>:\"|?*") != null;
}
pub fn startsWithDiskDiscriminator(maybe_path: []const u8) bool {
if (!bun.Environment.isWindows) return false;
if (maybe_path.len < 3) return false;
if (!isDriveLetter(maybe_path[0])) return false;
if (maybe_path[1] != ':') return false;
if (maybe_path[2] != '\\') return false;
return true;
}
// path.relative lets you do relative across different share drives
pub fn windowsFilesystemRootT(comptime T: type, path: []const T) []const T {
// minimum: `C:`

View File

@@ -1,14 +1,13 @@
import { spawn, spawnSync } from "bun";
import { afterEach, beforeEach, expect, it, describe } from "bun:test";
import { bunExe, bunEnv as env } from "harness";
import { mkdtemp, realpath, rm, mkdir, stat, exists } from "fs/promises";
import { tmpdir } from "os";
import { beforeEach, expect, it, describe } from "bun:test";
import { bunExe, bunEnv as env, tmpdirSync } from "harness";
import { mkdir, stat, exists } from "fs/promises";
import { join } from "path";
let x_dir: string;
beforeEach(async () => {
x_dir = await realpath(await mkdtemp(join(tmpdir(), "bun-x.test")));
x_dir = tmpdirSync("bun-create.test");
});
describe("should not crash", async () => {