From b399e7420c12ec37e25c3c5a6703fd772ee556e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=8BAndrzej=20Ressel?= Date: Wed, 18 Dec 2024 18:40:34 +0100 Subject: [PATCH] Rewrite test generation (#576) --- .github/workflows/build.yml | 7 +- .github/workflows/{deploy.yaml => deploy.yml} | 4 +- pulumi_wasm_generator_lib/tests/test.rs | 24 +++-- regenerate_providers/src/main.rs | 102 ++++++++++++++++++ 4 files changed, 121 insertions(+), 16 deletions(-) rename .github/workflows/{deploy.yaml => deploy.yml} (99%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 85d74a28..c57d1537 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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/ @@ -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 @@ -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/ @@ -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/short-sha@v3.0 id: short-sha with: diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yml similarity index 99% rename from .github/workflows/deploy.yaml rename to .github/workflows/deploy.yml index 028bd00a..b74de62e 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yml @@ -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/ @@ -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) diff --git a/pulumi_wasm_generator_lib/tests/test.rs b/pulumi_wasm_generator_lib/tests/test.rs index df92798d..3acd502a 100644 --- a/pulumi_wasm_generator_lib/tests/test.rs +++ b/pulumi_wasm_generator_lib/tests/test.rs @@ -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; +// 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] @@ -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"); @@ -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) diff --git a/regenerate_providers/src/main.rs b/regenerate_providers/src/main.rs index 6f00af75..efbf8446 100644 --- a/regenerate_providers/src/main.rs +++ b/regenerate_providers/src/main.rs @@ -1,3 +1,4 @@ +use std::collections::BTreeMap; use std::fs; use std::process::Command; @@ -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); @@ -41,6 +65,84 @@ 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]) {