Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): add fs module #4135

Merged
merged 30 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b0f5d5f
feat(sdk): add fs module
jianzs Oct 5, 2023
57d3753
Merge branch 'main' into add_fs_module
monadabot Oct 5, 2023
c606ad7
chore: self mutation (e2e-2of2.diff)
monadabot Oct 5, 2023
acff776
add @throws docstrings
jianzs Oct 6, 2023
2dd75cf
remove 'bring fs;' error from invalid/bring.test.w
jianzs Oct 7, 2023
4f2f6aa
Merge branch 'main' into add_fs_module
eladb Oct 10, 2023
07a028f
Merge branch 'main' into add_fs_module
monadabot Oct 10, 2023
9818e03
chore: self mutation (build.diff)
monadabot Oct 10, 2023
a3f7ee9
chore: self mutation (e2e-2of2.diff)
monadabot Oct 10, 2023
f73b06d
fix a bug: join failed on Windows
jianzs Oct 10, 2023
ef11be9
Merge branch 'add_fs_module' of github.com:jianzs/wing into add_fs_mo…
jianzs Oct 10, 2023
73c4cff
fix a bug: resolve failed on Windows
jianzs Oct 10, 2023
2265230
fix a bug: failed to handle the path starting from the root
jianzs Oct 10, 2023
690b0a6
Merge branch 'main' into add_fs_module
monadabot Oct 10, 2023
f6f8557
chore: self mutation (build.diff)
monadabot Oct 10, 2023
50d6ff9
keep the drive in the Windows posix-like path
jianzs Oct 11, 2023
f60b3e9
Merge branch 'main' into add_fs_module
monadabot Oct 11, 2023
4d66b1d
chore: self mutation (build.diff)
monadabot Oct 11, 2023
4aa8d6d
remove localPath
jianzs Oct 12, 2023
0705be2
use yaml instead of js-yaml
jianzs Oct 12, 2023
b04b3be
remove the @types/js-yaml dependency
jianzs Oct 12, 2023
f1be525
Merge branch 'main' into add_fs_module
monadabot Oct 12, 2023
d9b4484
chore: self mutation (build.diff)
monadabot Oct 12, 2023
b01c184
return undefined when any operation fails in tryX
jianzs Oct 12, 2023
4151dda
test fs methods in /tmp dir
jianzs Oct 12, 2023
ced77ae
set recursive default to true for mkdir
jianzs Oct 13, 2023
7807946
mkdtemp before test
jianzs Oct 13, 2023
26059f7
Merge branch 'main' into add_fs_module
monadabot Oct 13, 2023
b773835
chore: self mutation (e2e-1of2.diff)
monadabot Oct 13, 2023
094ee5f
chore: self mutation (e2e-2of2.diff)
monadabot Oct 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions examples/tests/invalid/bring.test.w
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ bring std;
bring cloud;
bring cloud;
// ^^^^^ "cloud" is already defined
bring fs;
// ^^^^^ "fs" is not a built-in module
bring ;
//^^^^^ Expected module specification (see https://www.winglang.io/docs/libraries)
bring c;
//^^^^^^ "c" is not a built-in module
//^^^^^^ "c" is not a built-in module
30 changes: 30 additions & 0 deletions examples/tests/sdk_tests/fs/basic.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
bring fs;

let filename = "hello.txt";
let data = "Hello, Wing!";

fs.writeFile(filename, data);
MarkMcCulloh marked this conversation as resolved.
Show resolved Hide resolved
assert(fs.exists(filename) == true);

let content = fs.readFile(filename);
assert(content == data);

fs.remove(filename);
assert(fs.exists(filename) == false);

let nilContent = fs.tryReadFile(filename);
assert(nilContent == nil);

test "inflight file basic operations" {
fs.writeFile(filename, data);
assert(fs.exists(filename) == true);

let content = fs.readFile(filename);
assert(content == data);

fs.remove(filename);
assert(fs.exists(filename) == false);

let nilContent = fs.tryReadFile(filename);
assert(nilContent == nil);
}
45 changes: 45 additions & 0 deletions examples/tests/sdk_tests/fs/directory.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
bring fs;
bring regex;

let dirname = "wingdir";
let filename = "temp.txt";

fs.mkdir(dirname);
assert(fs.exists(dirname) == true);

try {
fs.mkdir(dirname);
} catch e {
assert(regex.match("^EEXIST: file already exists", e) == true);
}

fs.writeFile(fs.join(dirname, filename), "");
let files = fs.readdir(dirname);
assert(files.length == 1);

fs.remove(dirname, { recursive: true });
assert(fs.exists(dirname) == false);

let nilFiles = fs.tryReaddir(dirname);
assert(nilFiles == nil);

test "inflight create normal directory" {
fs.mkdir(dirname);
assert(fs.exists(dirname) == true);

try {
fs.mkdir(dirname);
} catch e {
assert(regex.match("^EEXIST: file already exists", e) == true);
}

fs.writeFile(fs.join(dirname, filename), "");
let files = fs.readdir(dirname);
assert(files.length == 1);

fs.remove(dirname, { recursive: true });
assert(fs.exists(dirname) == false);

let nilFiles = fs.tryReaddir(dirname);
assert(nilFiles == nil);
}
46 changes: 46 additions & 0 deletions examples/tests/sdk_tests/fs/json.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
bring fs;
bring regex;

let filename = "test-json.json";
let data = Json {
"foo": "bar",
"arr": [1, 2, 3, "test", { "foo": "bar" }]
};

try {
fs.writeFile(filename, "invalid content");
fs.readJson(filename);
jianzs marked this conversation as resolved.
Show resolved Hide resolved
} catch e {
assert(regex.match("^Unexpected token", e) == true);
}

fs.writeJson(filename, data);
assert(fs.exists(filename) == true);

let obj = fs.readJson(filename);
assert(Json.stringify(obj) == Json.stringify(data));
eladb marked this conversation as resolved.
Show resolved Hide resolved

fs.remove(filename);
assert(fs.exists(filename) == false);

assert(fs.tryReadJson(filename) == nil);

test "inflight json operations" {
try {
fs.writeFile(filename, "invalid content");
fs.readJson(filename);
} catch e {
assert(regex.match("^Unexpected token", e) == true);
}

fs.writeJson(filename, data);
assert(fs.exists(filename) == true);

let obj = fs.readJson(filename);
assert(Json.stringify(obj) == Json.stringify(data));

fs.remove(filename);
assert(fs.exists(filename) == false);

assert(fs.tryReadJson(filename) == nil);
}
37 changes: 37 additions & 0 deletions examples/tests/sdk_tests/fs/path.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
bring fs;
bring regex;

let from = "/a/b/c/d";
let to = "/a/b/e/f";

let var result = fs.join(from, to);
assert(result == "/a/b/c/d/a/b/e/f");

result = fs.relative(from ,to);
assert(result == "../../e/f");

result = fs.dirname(from);
assert(result == "/a/b/c");

result = fs.basename(from);
assert(result == "d");

result = fs.resolve(from, to);
assert(regex.match("/a/b/e/f", result));

test "inflight path conversion" {
let var result = fs.join(from, to);
jianzs marked this conversation as resolved.
Show resolved Hide resolved
assert(result == "/a/b/c/d/a/b/e/f");

result = fs.relative(from ,to);
assert(result == "../../e/f");

result = fs.dirname(from);
assert(result == "/a/b/c");

result = fs.basename(from);
assert(result == "d");

result = fs.resolve(from, to);
assert(regex.match("/a/b/e/f", result));
}
15 changes: 15 additions & 0 deletions examples/tests/sdk_tests/fs/temp_dir.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
bring fs;

let tmpdir = fs.mkdtemp();
assert(fs.exists(tmpdir) == true);

fs.remove(tmpdir, { recursive: true });
assert(fs.exists(tmpdir) == false);

test "inflight create temporary directory" {
let tmpdir = fs.mkdtemp();
assert(fs.exists(tmpdir) == true);

fs.remove(tmpdir, { recursive: true });
assert(fs.exists(tmpdir) == false);
}
39 changes: 39 additions & 0 deletions examples/tests/sdk_tests/fs/yaml.main.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
bring fs;
bring regex;

let filename = "test-yaml.yaml";
let data = Json {
"foo": "bar",
"arr": [1, 2, 3, "test", { "foo": "bar" }]
};

try {
fs.writeFile(filename, "invalid: {{ content }}, invalid");
fs.readYaml(filename);
} catch e {
assert(regex.match("^bad indentation", e) == true);
}

fs.writeYaml(filename, data, data);
assert(fs.exists(filename) == true);

let objs = fs.readYaml(filename);
assert(objs.length == 2);
assert(Json.stringify(objs.at(0)) == Json.stringify(data));
assert(Json.stringify(objs.at(1)) == Json.stringify(data));

fs.remove(filename);
assert(fs.exists(filename) == false);

test "inflight yaml operations" {
fs.writeYaml(filename, data, data);
assert(fs.exists(filename) == true);

let objs = fs.readYaml(filename);
assert(objs.length == 2);
assert(Json.stringify(objs.at(0)) == Json.stringify(data));
assert(Json.stringify(objs.at(1)) == Json.stringify(data));

fs.remove(filename);
assert(fs.exists(filename) == false);
}
4 changes: 3 additions & 1 deletion libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,19 @@ const WINGSDK_MATH_MODULE: &'static str = "math";
const WINGSDK_AWS_MODULE: &'static str = "aws";
const WINGSDK_EX_MODULE: &'static str = "ex";
const WINGSDK_REGEX_MODULE: &'static str = "regex";
const WINGSDK_FS_MODULE: &'static str = "fs";

pub const UTIL_CLASS_NAME: &'static str = "Util";

const WINGSDK_BRINGABLE_MODULES: [&'static str; 7] = [
const WINGSDK_BRINGABLE_MODULES: [&'static str; 8] = [
WINGSDK_CLOUD_MODULE,
WINGSDK_UTIL_MODULE,
WINGSDK_HTTP_MODULE,
WINGSDK_MATH_MODULE,
WINGSDK_AWS_MODULE,
WINGSDK_EX_MODULE,
WINGSDK_REGEX_MODULE,
WINGSDK_FS_MODULE,
];

const WINGSDK_DURATION: &'static str = "std.Duration";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ source: libs/wingc/src/lsp/completions.rs
- label: ex
kind: 9
sortText: kk|ex
- label: fs
kind: 9
sortText: kk|fs
- label: http
kind: 9
sortText: kk|http
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ source: libs/wingc/src/lsp/completions.rs
- label: ex
kind: 9
sortText: kk|ex
- label: fs
kind: 9
sortText: kk|fs
- label: http
kind: 9
sortText: kk|http
Expand Down
10 changes: 10 additions & 0 deletions libs/wingsdk/.projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/wingsdk/.projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions libs/wingsdk/.projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ const project = new cdk.JsiiProject({
// shared client dependencies
"ioredis",
"jsonschema",
// fs module dependency
"js-yaml@^4.1.0",
"yaml",
],
devDeps: [
Expand All @@ -102,6 +104,7 @@ const project = new cdk.JsiiProject({
"vitest",
"@types/uuid",
"@vitest/coverage-v8",
"@types/js-yaml@^4.0.6",
"nanoid", // for ESM import test in target-sim/function.test.ts
...JSII_DEPS,
],
Expand Down
3 changes: 3 additions & 0 deletions libs/wingsdk/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading