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

[WIP] feat: cloud.Check (sdk modules written in Wing) #5427

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 25 additions & 12 deletions apps/wing/src/commands/pack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ describe("wing pack", () => {
"FavoriteNumbers",
"Store",
"default",
"sdk",
"subdir",
]
`);
Expand All @@ -192,21 +193,33 @@ describe("wing pack", () => {

expect(Object.keys(tarballContents).sort()).toMatchInlineSnapshot(`
[
"$lib/.wing/inflight.Store-2.js",
"$lib/.wing/inflight.Store-2.js.map",
"$lib/.wing/inflight.Util-1.js",
"$lib/.wing/inflight.Util-1.js.map",
"$lib/.wing/inflight.$Closure1-2.js",
"$lib/.wing/inflight.$Closure1-2.js.map",
"$lib/.wing/inflight.Check-2.js",
"$lib/.wing/inflight.Check-2.js.map",
"$lib/.wing/inflight.Results-1.js",
"$lib/.wing/inflight.Results-1.js.map",
"$lib/.wing/inflight.Store-4.js",
"$lib/.wing/inflight.Store-4.js.map",
"$lib/.wing/inflight.Util-3.js",
"$lib/.wing/inflight.Util-3.js.map",
"$lib/.wing/preflight.check-2.js",
"$lib/.wing/preflight.check-2.js.map",
"$lib/.wing/preflight.d.ts",
"$lib/.wing/preflight.enums-1.js",
"$lib/.wing/preflight.enums-1.js.map",
"$lib/.wing/preflight.enums-4.js",
"$lib/.wing/preflight.enums-4.js.map",
"$lib/.wing/preflight.js",
"$lib/.wing/preflight.js.map",
"$lib/.wing/preflight.store-3.js",
"$lib/.wing/preflight.store-3.js.map",
"$lib/.wing/preflight.subdir-4.js",
"$lib/.wing/preflight.subdir-4.js.map",
"$lib/.wing/preflight.util-2.js",
"$lib/.wing/preflight.util-2.js.map",
"$lib/.wing/preflight.results-1.js",
"$lib/.wing/preflight.results-1.js.map",
"$lib/.wing/preflight.sdk-3.js",
"$lib/.wing/preflight.sdk-3.js.map",
"$lib/.wing/preflight.store-6.js",
"$lib/.wing/preflight.store-6.js.map",
"$lib/.wing/preflight.subdir-7.js",
"$lib/.wing/preflight.subdir-7.js.map",
"$lib/.wing/preflight.util-5.js",
"$lib/.wing/preflight.util-5.js.map",
"LICENSE",
"README.md",
"enums.w",
Expand Down
12 changes: 12 additions & 0 deletions examples/tests/valid/sdk_in_wing.test.w
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
bring cloud;
bring http;

let api = new cloud.Api();

api.get("/", inflight (req) => {
return { status: 200 };
});

new cloud.Check(inflight () => {
assert(http.get(api.url).ok);
}) as "main route returns 200 ok";
11 changes: 10 additions & 1 deletion libs/wingc/examples/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@ pub fn main() {

let source_path = Utf8Path::new(&args[1]).canonicalize_utf8().unwrap();
let target_dir: Utf8PathBuf = env::current_dir().unwrap().join("target").try_into().unwrap();
let sdk_in_wing_dir = Utf8Path::new("../../libs/wingcompiler/sdk")
.canonicalize_utf8()
.unwrap();

let results = compile(source_path.parent().unwrap(), &source_path, None, &target_dir);
let results = compile(
source_path.parent().unwrap(),
&source_path,
None,
&target_dir,
&sdk_in_wing_dir,
);
if results.is_err() {
let mut diags = get_diagnostics();
// Sort error messages by line number (ascending)
Expand Down
81 changes: 44 additions & 37 deletions libs/wingc/src/file_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl FileGraph {
/// For example, if the current graph has file A depending on B, and
/// `update_file(pathA, &[pathC])` was called, then this function will remove the edge
/// from A to B, and add an edge from A to C.
pub fn update_file<'a, I: IntoIterator<Item = &'a Utf8PathBuf>>(&mut self, from_path: &Utf8Path, to_paths: I) {
pub fn set_file_deps<'a, I: IntoIterator<Item = &'a Utf8PathBuf>>(&mut self, from_path: &Utf8Path, to_paths: I) {
let from_node_index = self.get_or_insert_node_index(from_path);

// remove all current outcoming edges from this node
Expand All @@ -39,6 +39,13 @@ impl FileGraph {
}
}

/// Adds a file dependency to the graph.
pub fn add_file_dep(&mut self, from_path: &Utf8Path, to_path: &Utf8Path) {
let from_node_index = self.get_or_insert_node_index(from_path);
let to_node_index = self.get_or_insert_node_index(to_path);
self.graph.add_edge(from_node_index, to_node_index, ());
}

/// Returns true if the given file is in the graph
pub fn contains_file(&mut self, path: &Utf8Path) -> bool {
self.path_to_node_index.contains_key(path)
Expand Down Expand Up @@ -117,39 +124,39 @@ mod tests {
fn toposort_simple() {
// graph with two nodes, A and B, where A depends on B
let mut graph = FileGraph::default();
graph.update_file("a".into(), &["b".into()]);
graph.update_file("b".into(), &[]);
graph.set_file_deps("a".into(), &["b".into()]);
graph.set_file_deps("b".into(), &[]);
assert_eq!(graph.toposort().unwrap(), make_paths(&["b", "a"]));
}

#[test]
fn toposort_complex() {
// graph with 5 nodes, A, B, C, D, and E, where A depends on B and C, B depends on C and D, C depends on D, and D depends on E
let mut graph = FileGraph::default();
graph.update_file("a".into(), &["b".into(), "c".into()]);
graph.update_file("b".into(), &["c".into(), "d".into()]);
graph.update_file("c".into(), &["d".into()]);
graph.update_file("d".into(), &["e".into()]);
graph.update_file("e".into(), &[]);
graph.set_file_deps("a".into(), &["b".into(), "c".into()]);
graph.set_file_deps("b".into(), &["c".into(), "d".into()]);
graph.set_file_deps("c".into(), &["d".into()]);
graph.set_file_deps("d".into(), &["e".into()]);
graph.set_file_deps("e".into(), &[]);
assert_eq!(graph.toposort().unwrap(), make_paths(&["e", "d", "c", "b", "a"]));

// create the same graph in a different order as a sanity check
let mut graph = FileGraph::default();
graph.update_file("e".into(), &[]);
graph.update_file("d".into(), &["e".into()]);
graph.update_file("c".into(), &["d".into()]);
graph.update_file("b".into(), &["c".into(), "d".into()]);
graph.update_file("a".into(), &["b".into(), "c".into()]);
graph.set_file_deps("e".into(), &[]);
graph.set_file_deps("d".into(), &["e".into()]);
graph.set_file_deps("c".into(), &["d".into()]);
graph.set_file_deps("b".into(), &["c".into(), "d".into()]);
graph.set_file_deps("a".into(), &["b".into(), "c".into()]);
assert_eq!(graph.toposort().unwrap(), make_paths(&["e", "d", "c", "b", "a"]));
}

#[test]
fn toposort_cycle() {
// graph with 3 nodes, A, B, and C, where A depends on B, B depends on C, and C depends on A
let mut graph = FileGraph::default();
graph.update_file("a".into(), &["b".into()]);
graph.update_file("b".into(), &["c".into()]);
graph.update_file("c".into(), &["a".into()]);
graph.set_file_deps("a".into(), &["b".into()]);
graph.set_file_deps("b".into(), &["c".into()]);
graph.set_file_deps("c".into(), &["a".into()]);

let mut err = graph.toposort().unwrap_err();
err.sort();
Expand All @@ -160,11 +167,11 @@ mod tests {
fn toposort_two_cycles_with_shared_node() {
// graph where A is part of two cycles, {A,B,C} and {A,X,Y}
let mut graph = FileGraph::default();
graph.update_file("a".into(), &["b".into(), "x".into()]);
graph.update_file("b".into(), &["c".into()]);
graph.update_file("c".into(), &["a".into()]);
graph.update_file("x".into(), &["y".into()]);
graph.update_file("y".into(), &["a".into()]);
graph.set_file_deps("a".into(), &["b".into(), "x".into()]);
graph.set_file_deps("b".into(), &["c".into()]);
graph.set_file_deps("c".into(), &["a".into()]);
graph.set_file_deps("x".into(), &["y".into()]);
graph.set_file_deps("y".into(), &["a".into()]);

let mut err = graph.toposort().unwrap_err();
err.sort();
Expand All @@ -175,12 +182,12 @@ mod tests {
fn toposort_two_distinct_cycles() {
// graph with six nodes, where {A,B,C} form a cycle and {D,E,F} form a cycle
let mut graph = FileGraph::default();
graph.update_file("a".into(), &["b".into()]);
graph.update_file("b".into(), &["c".into()]);
graph.update_file("c".into(), &["a".into()]);
graph.update_file("d".into(), &["e".into()]);
graph.update_file("e".into(), &["f".into()]);
graph.update_file("f".into(), &["d".into()]);
graph.set_file_deps("a".into(), &["b".into()]);
graph.set_file_deps("b".into(), &["c".into()]);
graph.set_file_deps("c".into(), &["a".into()]);
graph.set_file_deps("d".into(), &["e".into()]);
graph.set_file_deps("e".into(), &["f".into()]);
graph.set_file_deps("f".into(), &["d".into()]);

let mut err = graph.toposort().unwrap_err();
err.sort();
Expand All @@ -193,11 +200,11 @@ mod tests {
fn toposort_cycle_and_unrelated_component() {
// graph with 5 nodes, where A depends on B, and {C,D,E} form a cycle
let mut graph = FileGraph::default();
graph.update_file("a".into(), &["b".into()]);
graph.update_file("b".into(), &[]);
graph.update_file("c".into(), &["d".into()]);
graph.update_file("d".into(), &["e".into()]);
graph.update_file("e".into(), &["c".into()]);
graph.set_file_deps("a".into(), &["b".into()]);
graph.set_file_deps("b".into(), &[]);
graph.set_file_deps("c".into(), &["d".into()]);
graph.set_file_deps("d".into(), &["e".into()]);
graph.set_file_deps("e".into(), &["c".into()]);

// we don't care about where the cycle starts, so we sort the list
let mut err = graph.toposort().unwrap_err();
Expand All @@ -209,14 +216,14 @@ mod tests {
fn toposort_update_edges() {
// graph with 3 nodes, A, B, and C, where A depends on B and C, and B depends on C
let mut graph = FileGraph::default();
graph.update_file("a".into(), &["b".into(), "c".into()]);
graph.update_file("b".into(), &["c".into()]);
graph.update_file("c".into(), &[]);
graph.set_file_deps("a".into(), &["b".into(), "c".into()]);
graph.set_file_deps("b".into(), &["c".into()]);
graph.set_file_deps("c".into(), &[]);
assert_eq!(graph.toposort().unwrap(), ["c", "b", "a"]);

// update the edges so that A depends on C and B depends on A
graph.update_file("a".into(), &["c".into()]);
graph.update_file("b".into(), &["a".into()]);
graph.set_file_deps("a".into(), &["c".into()]);
graph.set_file_deps("b".into(), &["a".into()]);
assert_eq!(graph.toposort().unwrap(), make_paths(&["c", "a", "b"]));
}

Expand Down
5 changes: 5 additions & 0 deletions libs/wingc/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ impl Files {
}
Ok(())
}

/// Get an iterator over all files.
pub fn keys(&self) -> impl Iterator<Item = &Utf8PathBuf> {
self.data.keys()
}
}

#[cfg(test)]
Expand Down
Loading
Loading