Skip to content

Commit

Permalink
Rewrite test generation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel committed Dec 18, 2024
1 parent f5a4681 commit 51fe9fa
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 19 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:
- uses: Swatinem/rust-cache@v2
with:
shared-key: build
workspaces: |
workspaces: | # DO NOT EDIT - START 1
./
pulumi_wasm_generator_lib/tests/output/array-of-enum-map/
pulumi_wasm_generator_lib/tests/output/azure-native-nested-types/
Expand All @@ -102,6 +102,7 @@ jobs:
pulumi_wasm_generator_lib/tests/output/output-funcs-edgeorder/
pulumi_wasm_generator_lib/tests/output/unions-inline/
pulumi_wasm_generator_lib/tests/output/unions-inside-arrays/
# DO NOT EDIT - END 1
- name: Regenerate provider list
run: just regenerate-provider-list
- name: Check
Expand Down Expand Up @@ -209,7 +210,7 @@ jobs:
with:
shared-key: build
save-if: false
workspaces: |
workspaces: | # DO NOT EDIT - START 2
./
pulumi_wasm_generator_lib/tests/output/array-of-enum-map/
pulumi_wasm_generator_lib/tests/output/azure-native-nested-types/
Expand All @@ -221,7 +222,7 @@ jobs:
pulumi_wasm_generator_lib/tests/output/output-funcs-edgeorder/
pulumi_wasm_generator_lib/tests/output/unions-inline/
pulumi_wasm_generator_lib/tests/output/unions-inside-arrays/
# DO NOT EDIT - END 2
- uses: benjlevesque/[email protected]
id: short-sha
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ jobs:
with:
shared-key: build
save-if: false
workspaces: |
workspaces: | # DO NOT EDIT - START 1
./
pulumi_wasm_generator_lib/tests/output/array-of-enum-map/
pulumi_wasm_generator_lib/tests/output/azure-native-nested-types/
Expand All @@ -270,7 +270,7 @@ jobs:
pulumi_wasm_generator_lib/tests/output/output-funcs-edgeorder/
pulumi_wasm_generator_lib/tests/output/unions-inline/
pulumi_wasm_generator_lib/tests/output/unions-inside-arrays/
# DO NOT EDIT - END 1
- name: Add target
run: rustup target add ${{ matrix.rust-target }}
- name: Build runner (Debug)
Expand Down
30 changes: 16 additions & 14 deletions pulumi_wasm_generator_lib/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;

use pulumi_wasm_generator_lib::{generate_rust_library, generate_wasm_provider};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use pulumi_wasm_generator_lib::{generate_rust_library, generate_wasm_provider};

// DO NOT EDIT - START

#[test]
fn array_of_enum_map() -> Result<()> {
run_pulumi_generator_test("array-of-enum-map", "example")
}

#[test]
fn different_enum() -> Result<()> {
run_pulumi_generator_test("different-enum", "plant")
fn cyclic_types() -> Result<()> {
run_pulumi_generator_test("cyclic-types", "example")
}

#[test]
fn mini_awsnative() -> Result<()> {
run_pulumi_generator_test("mini-awsnative", "aws-native")
fn different_enum() -> Result<()> {
run_pulumi_generator_test("different-enum", "plant")
}

#[test]
fn cyclic_types() -> Result<()> {
run_pulumi_generator_test("cyclic-types", "example")
fn functions_secrets() -> Result<()> {
run_pulumi_generator_test("functions-secrets", "mypkg")
}

#[test]
fn functions_secrets() -> Result<()> {
run_pulumi_generator_test("functions-secrets", "mypkg")
fn mini_awsnative() -> Result<()> {
run_pulumi_generator_test("mini-awsnative", "aws-native")
}

#[test]
Expand All @@ -50,9 +51,10 @@ fn unions_inline() -> Result<()> {
fn unions_inside_arrays() -> Result<()> {
run_pulumi_generator_test("unions-inside-arrays", "example")
}
// DO NOT EDIT - END

// provider_name is `name` from yaml file
fn run_pulumi_generator_test(test_name: &str, provider_name: &str) -> Result<()> {
pub fn run_pulumi_generator_test(test_name: &str, provider_name: &str) -> Result<()> {
let root_path = format!("tests/output/{test_name}");
let root = Path::new(&root_path);
let provider_output_path = root.join("provider");
Expand Down Expand Up @@ -99,7 +101,7 @@ fn run_pulumi_generator_test(test_name: &str, provider_name: &str) -> Result<()>
Ok(())
}

fn find_schema_files(name: &str) -> PathBuf {
pub fn find_schema_files(name: &str) -> PathBuf {
let possible_paths = vec![
Path::new("../pulumi/tests/testdata/codegen")
.join(name)
Expand Down
101 changes: 101 additions & 0 deletions regenerate_providers/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use std::fs;
use std::process::Command;

Expand All @@ -22,6 +23,29 @@ fn main() {
version: "5.43.1",
},
];
let tests = vec![
"array-of-enum-map",
"azure-native-nested-types",
"cyclic-types",
"different-enum",
"functions-secrets",
"mini-awsnative",
"output-funcs",
"output-funcs-edgeorder",
"unions-inline",
"unions-inside-arrays",
];
let test_map = BTreeMap::from([
("array-of-enum-map", "example"),
("different-enum", "plant"),
("mini-awsnative", "aws-native"),
("cyclic-types", "example"),
("functions-secrets", "mypkg"),
("output-funcs", "mypkg"),
("output-funcs-edgeorder", "myedgeorder"),
("unions-inline", "example"),
("unions-inside-arrays", "example"),
]);

for provider in &providers {
println!("{:?}", provider);
Expand All @@ -41,6 +65,83 @@ fn main() {

update_cargo_toml(&providers);
update_justfile(&providers);
update_tests(&tests, test_map);
}

fn update_tests(tests: &[&str], test_map: BTreeMap<&str, &str>) {
update_github_actions_build(tests);
update_github_actions_deploy(tests);
update_test_rs(test_map);
}

fn update_github_actions_build(tests: &[&str]) {
let content = fs::read_to_string(".github/workflows/deploy.yml")
.expect("Failed to read .github/workflows/deploy.yml");

let mut replacement = String::new();
replacement.push_str(" ./\n");
for test in tests {
replacement.push_str(&format!(
" pulumi_wasm_generator_lib/tests/output/{}/\n",
test
));
}
let start_marker = " # DO NOT EDIT - START 1";
let end_marker = "# DO NOT EDIT - END 1";
let content = replace_between_markers(&content, start_marker, end_marker, &replacement);

fs::write(".github/workflows/deploy.yml", content)
.expect("Failed to write to .github/workflows/deploy.yml");
}

fn update_github_actions_deploy(tests: &[&str]) {
let content = fs::read_to_string(".github/workflows/build.yml")
.expect("Failed to read .github/workflows/build.yml");

let mut replacement = String::new();
replacement.push_str(" ./\n");
for test in tests {
replacement.push_str(&format!(
" pulumi_wasm_generator_lib/tests/output/{}/\n",
test
));
}
let start_marker = " # DO NOT EDIT - START 1";
let end_marker = "# DO NOT EDIT - END 1";
let content = replace_between_markers(&content, start_marker, end_marker, &replacement);

let start_marker = " # DO NOT EDIT - START 2";
let end_marker = "# DO NOT EDIT - END 2";
let content = replace_between_markers(&content, start_marker, end_marker, &replacement);

fs::write(".github/workflows/build.yml", content)
.expect("Failed to write to .github/workflows/build.yml");
}

fn update_test_rs(tests: BTreeMap<&str, &str>) {
let content = fs::read_to_string("pulumi_wasm_generator_lib/tests/test.rs")
.expect("Failed to read pulumi_wasm_generator_lib/tests/test.rs");

let mut replacement = String::new();
for (test_directory, provider_name) in tests {

let method_name = test_directory.replace("-", "_");

let code = format!(r#"
#[test]
fn {method_name}() -> Result<()> {{
run_pulumi_generator_test("{test_directory}", "{provider_name}")
}}
"#);

replacement.push_str(&code);
}
let start_marker = "// DO NOT EDIT - START";
let end_marker = "// DO NOT EDIT - END";
let new_content = replace_between_markers(&content, start_marker, end_marker, &replacement);

fs::write("pulumi_wasm_generator_lib/tests/test.rs", new_content)
.expect("Failed to write to pulumi_wasm_generator_lib/tests/test.rs");
}

fn update_cargo_toml(providers: &[Provider]) {
Expand Down

0 comments on commit 51fe9fa

Please sign in to comment.