From 04e76c59315431a078b07034a3acc01301b20668 Mon Sep 17 00:00:00 2001 From: tknickman Date: Thu, 23 Jan 2025 23:02:29 -0500 Subject: [PATCH 1/3] feat(mfe): v2->v1 & make version optional --- crates/turborepo-lib/src/microfrontends.rs | 27 +--- .../src/task_graph/visitor/command.rs | 5 +- .../fixtures/sample.jsonc | 87 +++++------- .../turborepo-microfrontends/src/configv1.rs | 117 ++++++++++++++-- .../turborepo-microfrontends/src/configv2.rs | 120 ---------------- crates/turborepo-microfrontends/src/lib.rs | 128 +++++------------- 6 files changed, 179 insertions(+), 305 deletions(-) delete mode 100644 crates/turborepo-microfrontends/src/configv2.rs diff --git a/crates/turborepo-lib/src/microfrontends.rs b/crates/turborepo-lib/src/microfrontends.rs index 982f6aa20531d..b842a517b7171 100644 --- a/crates/turborepo-lib/src/microfrontends.rs +++ b/crates/turborepo-lib/src/microfrontends.rs @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet}; use itertools::Itertools; use tracing::warn; use turbopath::{AbsoluteSystemPath, RelativeUnixPath, RelativeUnixPathBuf}; -use turborepo_microfrontends::{Config as MFEConfig, Error, MICROFRONTENDS_PACKAGES}; +use turborepo_microfrontends::{Config as MFEConfig, Error, MICROFRONTENDS_PACKAGE}; use turborepo_repository::package_graph::{PackageGraph, PackageName}; use crate::{ @@ -194,11 +194,8 @@ impl PackageGraphResult { // We sort packages to ensure deterministic behavior let sorted_packages = packages.sorted_by(|(a, _), (b, _)| a.cmp(b)); for (package_name, config) in sorted_packages { - if let Some(pkg) = MICROFRONTENDS_PACKAGES - .iter() - .find(|static_pkg| package_name == **static_pkg) - { - mfe_package = Some(*pkg); + if package_name == MICROFRONTENDS_PACKAGE { + mfe_package = Some(MICROFRONTENDS_PACKAGE); } let Some(config) = config.or_else(|err| match err { @@ -266,9 +263,7 @@ impl ConfigInfo { mod test { use serde_json::json; use test_case::test_case; - use turborepo_microfrontends::{ - MICROFRONTENDS_PACKAGE_EXTERNAL, MICROFRONTENDS_PACKAGE_INTERNAL, - }; + use turborepo_microfrontends::MICROFRONTENDS_PACKAGE; use super::*; @@ -391,17 +386,9 @@ mod test { #[test] fn test_mfe_package_is_found() { - let result = PackageGraphResult::new( - vec![ - // These should never be present in the same graph, but if for some reason they - // are, we defer to the external variant. - (MICROFRONTENDS_PACKAGE_EXTERNAL, Ok(None)), - (MICROFRONTENDS_PACKAGE_INTERNAL, Ok(None)), - ] - .into_iter(), - ) - .unwrap(); - assert_eq!(result.mfe_package, Some(MICROFRONTENDS_PACKAGE_EXTERNAL)); + let result = + PackageGraphResult::new(vec![(MICROFRONTENDS_PACKAGE, Ok(None))].into_iter()).unwrap(); + assert_eq!(result.mfe_package, Some(MICROFRONTENDS_PACKAGE)); } #[test] diff --git a/crates/turborepo-lib/src/task_graph/visitor/command.rs b/crates/turborepo-lib/src/task_graph/visitor/command.rs index 9ba3bd5bf69d8..ccf14ecfabaf4 100644 --- a/crates/turborepo-lib/src/task_graph/visitor/command.rs +++ b/crates/turborepo-lib/src/task_graph/visitor/command.rs @@ -2,7 +2,7 @@ use std::{collections::HashSet, path::PathBuf}; use turbopath::AbsoluteSystemPath; use turborepo_env::EnvironmentVariableMap; -use turborepo_microfrontends::MICROFRONTENDS_PACKAGES; +use turborepo_microfrontends::MICROFRONTENDS_PACKAGE; use turborepo_repository::package_graph::{PackageGraph, PackageInfo, PackageName}; use super::Error; @@ -206,7 +206,8 @@ impl<'a> CommandProvider for MicroFrontendProxyProvider<'a> { let has_mfe_dependency = package_info .package_json .all_dependencies() - .any(|(package, _version)| MICROFRONTENDS_PACKAGES.contains(&package.as_str())); + .any(|(package, _version)| package.as_str() == MICROFRONTENDS_PACKAGE); + if !has_mfe_dependency { let mfe_config_filename = self.mfe_configs.config_filename(task_id.package()); return Err(Error::MissingMFEDependency { diff --git a/crates/turborepo-microfrontends/fixtures/sample.jsonc b/crates/turborepo-microfrontends/fixtures/sample.jsonc index 38ef07308d98b..51307268fba35 100644 --- a/crates/turborepo-microfrontends/fixtures/sample.jsonc +++ b/crates/turborepo-microfrontends/fixtures/sample.jsonc @@ -2,7 +2,6 @@ "version": "1", "applications": { "main-site": { - "default": true, "development": { "local": { "protocol": "http", @@ -19,40 +18,35 @@ "host": "main.com" }, "vercel": { - "projectId": "id1", - "projectName": "main-site" + "projectId": "id1" } }, "vercel-marketing": { - "default": false, - "routing": { - "assetPrefix": "market-site", - "matches": [ - { - "group": "blog", - "paths": [ - "/blog", - "/blog/:slug*", - "/press", - "/changelog", - "/changelog/:slug*", - "/customers/:slug*" - ] - }, - { - "group": "navbar", - "paths": [ - "/", - "/contact", - "/pricing", - "/enterprise", - // Resources - "/customers", - "/solutions/composable-commerce" - ] - } - ] - }, + "routing": [ + { + "group": "blog", + "paths": [ + "/blog", + "/blog/:slug*", + "/press", + "/changelog", + "/changelog/:slug*", + "/customers/:slug*" + ] + }, + { + "group": "navbar", + "paths": [ + "/", + "/contact", + "/pricing", + "/enterprise", + // Resources + "/customers", + "/solutions/composable-commerce" + ] + } + ], "development": { "local": { "protocol": "http", @@ -69,20 +63,15 @@ "host": "market.main.com" }, "vercel": { - "projectId": "id2", - "projectName": "market-site" + "projectId": "id2" } }, "foo-docs": { - "default": false, - "routing": { - "assetPrefix": "foo", - "matches": [ - { - "paths": ["/foo/:path*"] - } - ] - }, + "routing": [ + { + "paths": ["/foo/:path*"] + } + ], "development": { "fallback": { "protocol": "https", @@ -99,16 +88,11 @@ "host": "foo.main.com" }, "vercel": { - "projectId": "id3", - "projectName": "foo-docs" + "projectId": "id3" } }, "docs": { - "default": false, - "routing": { - "assetPrefix": "docs", - "matches": [] - }, + "routing": [], "development": { "fallback": { "protocol": "https", @@ -125,8 +109,7 @@ "host": "docs.main.com" }, "vercel": { - "projectId": "id4", - "projectName": "docs-site" + "projectId": "id4" } } } diff --git a/crates/turborepo-microfrontends/src/configv1.rs b/crates/turborepo-microfrontends/src/configv1.rs index 83a98b6fb5a5c..4bdf8c919105b 100644 --- a/crates/turborepo-microfrontends/src/configv1.rs +++ b/crates/turborepo-microfrontends/src/configv1.rs @@ -6,40 +6,61 @@ use serde::Serialize; use crate::Error; -/// The minimal amount of information Turborepo needs to correctly start a local -/// proxy server for microfrontends +pub enum ParseResult { + Actual(ConfigV1), + Reference(String), +} + #[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] pub struct ConfigV1 { - pub version: String, - pub applications: BTreeMap, + version: Option, + applications: BTreeMap, +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] +struct ChildConfig { + part_of: String, } #[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] -pub struct Application { - pub development: Development, +struct Application { + development: Option, } #[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] -pub struct Development { - #[serde(skip_serializing_if = "Option::is_none")] - pub task: Option, +struct Development { + task: Option, } impl ConfigV1 { - pub fn from_str(input: &str, source: &str) -> Result { - let (config, errs) = biome_deserialize::json::deserialize_from_json_str::( + pub fn from_str(input: &str, source: &str) -> Result { + // attempt to parse a child, ignoring any errors + let (config, errs) = biome_deserialize::json::deserialize_from_json_str::( + input, + JsonParserOptions::default().with_allow_comments(), + source, + ) + .consume(); + + if let Some(ChildConfig { part_of }) = errs.is_empty().then_some(config).flatten() { + return Ok(ParseResult::Reference(part_of)); + } + // attempt to parse a real one + let (config, errs) = biome_deserialize::json::deserialize_from_json_str::( input, JsonParserOptions::default().with_allow_comments(), source, ) .consume(); + if let Some(config) = config { - if config.version == "1" { - Ok(config) + let version = config.version.clone().unwrap_or("1".to_string()); + if version == "1" { + Ok(ParseResult::Actual(config)) } else { Err(Error::InvalidVersion { expected: "1", - actual: config.version, + actual: version, }) } } else { @@ -50,6 +71,72 @@ impl ConfigV1 { pub fn development_tasks(&self) -> impl Iterator)> { self.applications .iter() - .map(|(application, config)| (application.as_str(), config.development.task.as_deref())) + .map(|(application, config)| (application.as_str(), config.task())) + } +} + +impl Application { + fn task(&self) -> Option<&str> { + self.development.as_ref()?.task.as_deref() + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_child_config_parse() { + let input = r#"{"partOf": "web"}"#; + let config = ConfigV1::from_str(input, "somewhere").unwrap(); + match config { + ParseResult::Actual(_config_v2) => panic!("expected to get reference to default app"), + ParseResult::Reference(default_app) => { + assert_eq!(default_app, "web"); + } + } + } + + #[test] + fn test_root_config_parse() { + let input = r#"{ + "version": "1", + "applications": { + "web": {}, + "docs": {"development": {"task": "serve"}} + } + }"#; + let config = ConfigV1::from_str(input, "somewhere").unwrap(); + match config { + ParseResult::Actual(config_v1) => { + assert_eq!(config_v1.applications.get("web").unwrap().task(), None); + assert_eq!( + config_v1.applications.get("docs").unwrap().task(), + Some("serve") + ); + } + ParseResult::Reference(_) => panic!("expected to get main config"), + } + } + + #[test] + fn test_no_version_config_parse() { + let input = r#"{ + "applications": { + "web": {}, + "docs": {"development": {"task": "serve"}} + } + }"#; + let config = ConfigV1::from_str(input, "somewhere").unwrap(); + match config { + ParseResult::Actual(config_v1) => { + assert_eq!(config_v1.applications.get("web").unwrap().task(), None); + assert_eq!( + config_v1.applications.get("docs").unwrap().task(), + Some("serve") + ); + } + ParseResult::Reference(_) => panic!("expected to get main config"), + } } } diff --git a/crates/turborepo-microfrontends/src/configv2.rs b/crates/turborepo-microfrontends/src/configv2.rs deleted file mode 100644 index 744f741b4e967..0000000000000 --- a/crates/turborepo-microfrontends/src/configv2.rs +++ /dev/null @@ -1,120 +0,0 @@ -use std::collections::BTreeMap; - -use biome_deserialize_macros::Deserializable; -use biome_json_parser::JsonParserOptions; -use serde::Serialize; - -use crate::Error; - -pub enum ParseResult { - Actual(ConfigV2), - Reference(String), -} - -#[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] -pub struct ConfigV2 { - version: String, - applications: BTreeMap, -} - -#[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] -struct ChildConfig { - part_of: String, -} - -#[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] -struct Application { - development: Option, -} - -#[derive(Debug, PartialEq, Eq, Serialize, Deserializable, Default)] -struct Development { - task: Option, -} - -impl ConfigV2 { - pub fn from_str(input: &str, source: &str) -> Result { - // attempt to parse a child, ignoring any errors - let (config, errs) = biome_deserialize::json::deserialize_from_json_str::( - input, - JsonParserOptions::default().with_allow_comments(), - source, - ) - .consume(); - - if let Some(ChildConfig { part_of }) = errs.is_empty().then_some(config).flatten() { - return Ok(ParseResult::Reference(part_of)); - } - // attempt to parse a real one - let (config, errs) = biome_deserialize::json::deserialize_from_json_str::( - input, - JsonParserOptions::default().with_allow_comments(), - source, - ) - .consume(); - - if let Some(config) = config { - if config.version == "2" { - Ok(ParseResult::Actual(config)) - } else { - Err(Error::InvalidVersion { - expected: "2", - actual: config.version, - }) - } - } else { - Err(Error::biome_error(errs)) - } - } - - pub fn development_tasks(&self) -> impl Iterator)> { - self.applications - .iter() - .map(|(application, config)| (application.as_str(), config.task())) - } -} - -impl Application { - fn task(&self) -> Option<&str> { - self.development.as_ref()?.task.as_deref() - } -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_child_config_parse() { - let input = r#"{"partOf": "web"}"#; - let config = ConfigV2::from_str(input, "somewhere").unwrap(); - match config { - ParseResult::Actual(_config_v2) => panic!("expected to get reference to default app"), - ParseResult::Reference(default_app) => { - assert_eq!(default_app, "web"); - } - } - } - - #[test] - fn test_root_config_parse() { - let input = r#"{ - "version": "2", - "applications": { - "web": {}, - "docs": {"development": {"task": "serve"}} - } - }"#; - let config = ConfigV2::from_str(input, "somewhere").unwrap(); - match config { - ParseResult::Actual(config_v2) => { - assert_eq!(config_v2.applications.get("web").unwrap().task(), None); - assert_eq!( - config_v2.applications.get("docs").unwrap().task(), - Some("serve") - ); - } - ParseResult::Reference(_) => panic!("expected to get main config"), - } - } -} diff --git a/crates/turborepo-microfrontends/src/lib.rs b/crates/turborepo-microfrontends/src/lib.rs index 9ea35114d489b..fde2ecd891390 100644 --- a/crates/turborepo-microfrontends/src/lib.rs +++ b/crates/turborepo-microfrontends/src/lib.rs @@ -1,6 +1,5 @@ #![deny(clippy::all)] mod configv1; -mod configv2; mod error; use std::io; @@ -8,7 +7,6 @@ use std::io; use biome_deserialize_macros::Deserializable; use biome_json_parser::JsonParserOptions; use configv1::ConfigV1; -use configv2::ConfigV2; pub use error::Error; use turbopath::{ AbsoluteSystemPath, AbsoluteSystemPathBuf, AnchoredSystemPath, AnchoredSystemPathBuf, @@ -17,17 +15,10 @@ use turbopath::{ /// Currently the default path for a package that provides a configuration. /// /// This is subject to change at any time. -pub const DEFAULT_MICROFRONTENDS_CONFIG_V1: &str = "micro-frontends.jsonc"; -pub const DEFAULT_MICROFRONTENDS_CONFIG_V2: &str = "microfrontends.json"; -pub const DEFAULT_MICROFRONTENDS_CONFIG_V2_ALT: &str = "microfrontends.jsonc"; -pub const MICROFRONTENDS_PACKAGES: &[&str] = [ - MICROFRONTENDS_PACKAGE_EXTERNAL, - MICROFRONTENDS_PACKAGE_INTERNAL, -] -.as_slice(); -pub const MICROFRONTENDS_PACKAGE_INTERNAL: &str = "@vercel/micro-frontends-internal"; -pub const MICROFRONTENDS_PACKAGE_EXTERNAL: &str = "@vercel/microfrontends"; -pub const SUPPORTED_VERSIONS: &[&str] = ["1", "2"].as_slice(); +pub const DEFAULT_MICROFRONTENDS_CONFIG_V1: &str = "microfrontends.json"; +pub const DEFAULT_MICROFRONTENDS_CONFIG_V1_ALT: &str = "microfrontends.jsonc"; +pub const MICROFRONTENDS_PACKAGE: &str = "@vercel/microfrontends"; +pub const SUPPORTED_VERSIONS: &[&str] = ["1"].as_slice(); /// The minimal amount of information Turborepo needs to correctly start a local /// proxy server for microfrontends @@ -41,7 +32,6 @@ pub struct Config { #[derive(Debug, PartialEq, Eq)] enum ConfigInner { V1(ConfigV1), - V2(ConfigV2), } impl Config { @@ -62,7 +52,7 @@ impl Config { package_dir: &AnchoredSystemPath, ) -> Result, Error> { let absolute_dir = repo_root.resolve(package_dir); - let mut config = if let Some(config) = Self::load_v2_dir(&absolute_dir)? { + let mut config = if let Some(config) = Self::load_v1_dir(&absolute_dir)? { Ok(Some(config)) } else { Self::load_v1_dir(&absolute_dir) @@ -87,15 +77,14 @@ impl Config { let version = match version_only { Some(VersionOnly { version }) => version, - // Default to version 2 if no version found - None => "2".to_string(), + // Default to version 1 if no version found + None => "1".to_string(), }; let inner = match version.as_str() { - "1" => ConfigV1::from_str(input, source).map(ConfigInner::V1), - "2" => ConfigV2::from_str(input, source).and_then(|result| match result { - configv2::ParseResult::Actual(config_v2) => Ok(ConfigInner::V2(config_v2)), - configv2::ParseResult::Reference(default_app) => Err(Error::ChildConfig { + "1" => ConfigV1::from_str(input, source).and_then(|result| match result { + configv1::ParseResult::Actual(config_v1) => Ok(ConfigInner::V1(config_v1)), + configv1::ParseResult::Reference(default_app) => Err(Error::ChildConfig { reference: default_app, }), }), @@ -113,7 +102,6 @@ impl Config { ) -> Box)> + 'a> { match &self.inner { ConfigInner::V1(config_v1) => Box::new(config_v1.development_tasks()), - ConfigInner::V2(config_v2) => Box::new(config_v2.development_tasks()), } } @@ -130,56 +118,40 @@ impl Config { pub fn version(&self) -> &'static str { match &self.inner { ConfigInner::V1(_) => "1", - ConfigInner::V2(_) => "2", } } - fn load_v2_dir(dir: &AbsoluteSystemPath) -> Result, Error> { + fn load_v1_dir(dir: &AbsoluteSystemPath) -> Result, Error> { let load_config = |filename: &str| -> Option<(Result, AbsoluteSystemPathBuf)> { let path = dir.join_component(filename); let contents = path.read_existing_to_string().transpose()?; Some((contents, path)) }; - let Some((contents, path)) = load_config(DEFAULT_MICROFRONTENDS_CONFIG_V2) - .or_else(|| load_config(DEFAULT_MICROFRONTENDS_CONFIG_V2_ALT)) + let Some((contents, path)) = load_config(DEFAULT_MICROFRONTENDS_CONFIG_V1) + .or_else(|| load_config(DEFAULT_MICROFRONTENDS_CONFIG_V1_ALT)) else { return Ok(None); }; let contents = contents?; - ConfigV2::from_str(&contents, path.as_str()) + ConfigV1::from_str(&contents, path.as_str()) .and_then(|result| match result { - configv2::ParseResult::Actual(config_v2) => Ok(Config { - inner: ConfigInner::V2(config_v2), + configv1::ParseResult::Actual(config_v1) => Ok(Config { + inner: ConfigInner::V1(config_v1), filename: path .file_name() .expect("microfrontends config should not be root") .to_owned(), path: None, }), - configv2::ParseResult::Reference(default_app) => Err(Error::ChildConfig { + configv1::ParseResult::Reference(default_app) => Err(Error::ChildConfig { reference: default_app, }), }) .map(Some) } - fn load_v1_dir(dir: &AbsoluteSystemPath) -> Result, Error> { - let path = dir.join_component(DEFAULT_MICROFRONTENDS_CONFIG_V1); - let Some(contents) = path.read_existing_to_string()? else { - return Ok(None); - }; - - ConfigV1::from_str(&contents, path.as_str()) - .map(|config_v1| Self { - inner: ConfigInner::V1(config_v1), - filename: DEFAULT_MICROFRONTENDS_CONFIG_V1.to_owned(), - path: None, - }) - .map(Some) - } - /// Sets the path the configuration was loaded from pub fn set_path(&mut self, dir: &AnchoredSystemPath) { self.path = Some(dir.join_component(&self.filename)); @@ -205,7 +177,7 @@ mod test { fn test_unsupported_version() { let input = r#"{"version": "yolo"}"#; let err = Config::from_str(input, "something.json").unwrap_err(); - assert_snapshot!(err, @r###"Unsupported microfrontends configuration version: yolo. Supported versions: ["1", "2"]"###); + assert_snapshot!(err, @r###"Unsupported microfrontends configuration version: yolo. Supported versions: ["1"]"###); } fn add_v1_config(dir: &AbsoluteSystemPath) -> Result<(), std::io::Error> { @@ -214,22 +186,15 @@ mod test { path.create_with_contents(r#"{"version": "1", "applications": {"web": {"development": {"task": "serve"}}, "docs": {}}}"#) } - fn add_v2_config(dir: &AbsoluteSystemPath) -> Result<(), std::io::Error> { - let path = dir.join_component(DEFAULT_MICROFRONTENDS_CONFIG_V2); + fn add_v1_alt_config(dir: &AbsoluteSystemPath) -> Result<(), std::io::Error> { + let path = dir.join_component(DEFAULT_MICROFRONTENDS_CONFIG_V1_ALT); path.ensure_dir()?; - path.create_with_contents(r#"{"version": "2", "applications": {"web": {"development": {"task": "serve"}}, "docs": {}}}"#) - } - - fn add_v2_alt_config(dir: &AbsoluteSystemPath) -> Result<(), std::io::Error> { - let path = dir.join_component(DEFAULT_MICROFRONTENDS_CONFIG_V2_ALT); - path.ensure_dir()?; - path.create_with_contents(r#"{"version": "2", "applications": {"web": {"development": {"task": "serve"}}, "docs": {}}}"#) + path.create_with_contents(r#"{"version": "1", "applications": {"web": {"development": {"task": "serve"}}, "docs": {}}}"#) } struct LoadDirTest { has_v1: bool, - has_v2: bool, - has_alt_v2: bool, + has_alt_v1: bool, pkg_dir: &'static str, expected_version: Option, expected_filename: Option<&'static str>, @@ -238,7 +203,6 @@ mod test { #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum FoundConfig { V1, - V2, } impl LoadDirTest { @@ -246,8 +210,7 @@ mod test { Self { pkg_dir, has_v1: false, - has_v2: false, - has_alt_v2: false, + has_alt_v1: false, expected_version: None, expected_filename: None, } @@ -258,13 +221,8 @@ mod test { self } - pub const fn has_v2(mut self) -> Self { - self.has_v2 = true; - self - } - - pub const fn has_alt_v2(mut self) -> Self { - self.has_alt_v2 = true; + pub const fn has_alt_v1(mut self) -> Self { + self.has_alt_v1 = true; self } @@ -273,11 +231,6 @@ mod test { self } - pub const fn expects_v2(mut self) -> Self { - self.expected_version = Some(FoundConfig::V2); - self - } - pub const fn with_filename(mut self, filename: &'static str) -> Self { self.expected_filename = Some(filename); self @@ -298,27 +251,14 @@ mod test { .expects_v1() .with_filename(DEFAULT_MICROFRONTENDS_CONFIG_V1); - const LOAD_V2: LoadDirTest = LoadDirTest::new("web") - .has_v2() - .expects_v2() - .with_filename(DEFAULT_MICROFRONTENDS_CONFIG_V2); - - const LOAD_BOTH: LoadDirTest = LoadDirTest::new("web") - .has_v1() - .has_v2() - .expects_v2() - .with_filename(DEFAULT_MICROFRONTENDS_CONFIG_V2); - - const LOAD_V2_ALT: LoadDirTest = LoadDirTest::new("web") - .has_alt_v2() - .expects_v2() - .with_filename(DEFAULT_MICROFRONTENDS_CONFIG_V2_ALT); + const LOAD_V1_ALT: LoadDirTest = LoadDirTest::new("web") + .has_alt_v1() + .expects_v1() + .with_filename(DEFAULT_MICROFRONTENDS_CONFIG_V1_ALT); const LOAD_NONE: LoadDirTest = LoadDirTest::new("web"); #[test_case(LOAD_V1)] - #[test_case(LOAD_V2)] - #[test_case(LOAD_BOTH)] - #[test_case(LOAD_V2_ALT)] + #[test_case(LOAD_V1_ALT)] #[test_case(LOAD_NONE)] fn test_load_dir(case: LoadDirTest) { let dir = TempDir::new().unwrap(); @@ -328,17 +268,13 @@ mod test { if case.has_v1 { add_v1_config(&pkg_path).unwrap(); } - if case.has_v2 { - add_v2_config(&pkg_path).unwrap(); - } - if case.has_alt_v2 { - add_v2_alt_config(&pkg_path).unwrap(); + if case.has_alt_v1 { + add_v1_alt_config(&pkg_path).unwrap(); } let config = Config::load_from_dir(repo_root, pkg_dir).unwrap(); let actual_version = config.as_ref().map(|config| match &config.inner { ConfigInner::V1(_) => FoundConfig::V1, - ConfigInner::V2(_) => FoundConfig::V2, }); let actual_path = config.as_ref().and_then(|config| config.path()); assert_eq!(actual_version, case.expected_version); From 28426ffced0a38b3eceaace2f07bf22c1417a641 Mon Sep 17 00:00:00 2001 From: tknickman Date: Fri, 24 Jan 2025 08:44:38 -0500 Subject: [PATCH 2/3] fix remaining tests --- crates/turborepo-lib/src/microfrontends.rs | 58 +------------------ crates/turborepo-lib/src/turbo_json/loader.rs | 2 +- 2 files changed, 4 insertions(+), 56 deletions(-) diff --git a/crates/turborepo-lib/src/microfrontends.rs b/crates/turborepo-lib/src/microfrontends.rs index b842a517b7171..54d36211cf128 100644 --- a/crates/turborepo-lib/src/microfrontends.rs +++ b/crates/turborepo-lib/src/microfrontends.rs @@ -276,7 +276,7 @@ mod test { for _dev_task in $dev_tasks.as_slice() { _dev_tasks.insert(crate::run::task_id::TaskName::from(*_dev_task).task_id().unwrap().into_owned()); } - _map.insert($config_owner.to_string(), ConfigInfo { tasks: _dev_tasks, version: "2", path: None }); + _map.insert($config_owner.to_string(), ConfigInfo { tasks: _dev_tasks, version: "1", path: None }); )+ _map } @@ -298,7 +298,7 @@ mod test { pub const fn new(package_name: &'static str) -> Self { Self { package_name, - version: "2", + version: "1", result: None, } } @@ -451,7 +451,7 @@ mod test { fn test_dev_task_collection() { let config = MFEConfig::from_str( &serde_json::to_string_pretty(&json!({ - "version": "2", + "version": "1", "applications": { "web": {}, "docs": { @@ -495,56 +495,4 @@ mod test { .unwrap(); assert_eq!(actual.global_deps, &["web/microfrontends.json".to_owned()]); } - - #[test] - fn test_v2_and_v1() { - let config_v2 = MFEConfig::from_str( - &serde_json::to_string_pretty(&json!({ - "version": "2", - "applications": { - "web": {}, - "docs": { - "development": { - "task": "serve" - } - } - } - })) - .unwrap(), - "something.txt", - ) - .unwrap(); - let config_v1 = MFEConfig::from_str( - &serde_json::to_string_pretty(&json!({ - "version": "1", - "applications": { - "web": {}, - "docs": { - "development": { - "task": "serve" - } - } - } - })) - .unwrap(), - "something.txt", - ) - .unwrap(); - let result = PackageGraphResult::new( - vec![ - ("web", Ok(Some(config_v2))), - ("docs", Ok(None)), - ("mfe-config", Ok(Some(config_v1))), - ] - .into_iter(), - ) - .unwrap(); - let mut expected = mfe_configs!( - "web" => ["web#dev", "docs#serve"], - "mfe-config" => ["web#dev", "docs#serve"] - ); - expected.get_mut("mfe-config").unwrap().version = "1"; - - assert_eq!(result.configs, expected,) - } } diff --git a/crates/turborepo-lib/src/turbo_json/loader.rs b/crates/turborepo-lib/src/turbo_json/loader.rs index b296c36bdacb0..01bc26f1e61b5 100644 --- a/crates/turborepo-lib/src/turbo_json/loader.rs +++ b/crates/turborepo-lib/src/turbo_json/loader.rs @@ -755,7 +755,7 @@ mod test { ( "web", turborepo_microfrontends::Config::from_str( - r#"{"version": "2", "applications": {"web": {}, "docs": {}}}"#, + r#"{"version": "1", "applications": {"web": {}, "docs": {}}}"#, "mfe.json", ) .map(Some), From 1adc94de1fdabccd278b74c4aba73c74f6494caf Mon Sep 17 00:00:00 2001 From: tknickman Date: Fri, 24 Jan 2025 11:17:35 -0500 Subject: [PATCH 3/3] remote old tests --- crates/turborepo-lib/src/microfrontends.rs | 33 ---------------------- 1 file changed, 33 deletions(-) diff --git a/crates/turborepo-lib/src/microfrontends.rs b/crates/turborepo-lib/src/microfrontends.rs index 54d36211cf128..7c2f8d19d0468 100644 --- a/crates/turborepo-lib/src/microfrontends.rs +++ b/crates/turborepo-lib/src/microfrontends.rs @@ -262,7 +262,6 @@ impl ConfigInfo { #[cfg(test)] mod test { use serde_json::json; - use test_case::test_case; use turborepo_microfrontends::MICROFRONTENDS_PACKAGE; use super::*; @@ -352,38 +351,6 @@ mod test { } } - const NON_MFE_PKG: PackageUpdateTest = PackageUpdateTest::new("other-pkg"); - const MFE_CONFIG_PKG: PackageUpdateTest = PackageUpdateTest::new("z-config-pkg") - .v1() - .proxy_only("z-config-pkg#proxy"); - const MFE_CONFIG_PKG_DEV_TASK: PackageUpdateTest = - PackageUpdateTest::new("web").dev("web#dev", "web#proxy"); - const DEFAULT_APP_PROXY: PackageUpdateTest = - PackageUpdateTest::new("docs").dev("docs#serve", "web#proxy"); - const DEFAULT_APP_PROXY_AND_DEV: PackageUpdateTest = - PackageUpdateTest::new("web").dev("web#dev", "web#proxy"); - - #[test_case(NON_MFE_PKG)] - #[test_case(MFE_CONFIG_PKG)] - #[test_case(MFE_CONFIG_PKG_DEV_TASK)] - #[test_case(DEFAULT_APP_PROXY)] - #[test_case(DEFAULT_APP_PROXY_AND_DEV)] - fn test_package_turbo_json_update(test: PackageUpdateTest) { - let mut configs = mfe_configs!( - "z-config-pkg" => ["web#dev", "docs#dev"], - "web" => ["web#dev", "docs#serve"] - ); - configs.get_mut("z-config-pkg").unwrap().version = "1"; - let mfe = MicrofrontendsConfigs { - configs, - mfe_package: None, - }; - assert_eq!( - mfe.package_turbo_json_update(&test.package_name()), - test.expected() - ); - } - #[test] fn test_mfe_package_is_found() { let result =