这是indexloc提供的服务,不要输入任何密码
Skip to content

fix(mfe): properly handle versionless configuration #9945

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

Merged
merged 2 commits into from
Feb 12, 2025
Merged
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
33 changes: 30 additions & 3 deletions crates/turborepo-microfrontends/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Config {
pub fn from_str(input: &str, source: &str) -> Result<Self, Error> {
#[derive(Deserializable, Default)]
struct VersionOnly {
version: String,
version: Option<String>,
}
let (version_only, _errs) = biome_deserialize::json::deserialize_from_json_str(
input,
Expand All @@ -80,9 +80,11 @@ impl Config {
.consume();

let version = match version_only {
Some(VersionOnly { version }) => version,
Some(VersionOnly {
version: Some(version),
}) => version,
// Default to version 1 if no version found
None => "1".to_string(),
Some(VersionOnly { version: None }) | None => "1".to_string(),
};

let inner = match version.as_str() {
Expand Down Expand Up @@ -180,6 +182,14 @@ mod test {
path.create_with_contents(r#"{"version": "1", "applications": {"web": {"development": {"task": "serve"}}, "docs": {}}}"#)
}

fn add_no_version_config(dir: &AbsoluteSystemPath) -> Result<(), std::io::Error> {
let path = dir.join_component(DEFAULT_MICROFRONTENDS_CONFIG_V1);
path.ensure_dir()?;
path.create_with_contents(
r#"{"applications": {"web": {"development": {"task": "serve"}}, "docs": {}}}"#,
)
}

fn add_v2_config(dir: &AbsoluteSystemPath) -> Result<(), std::io::Error> {
let path = dir.join_component(DEFAULT_MICROFRONTENDS_CONFIG_V1);
path.ensure_dir()?;
Expand All @@ -195,6 +205,7 @@ mod test {
struct LoadDirTest {
has_v1: bool,
has_alt_v1: bool,
has_versionless: bool,
pkg_dir: &'static str,
expected_version: Option<FoundConfig>,
expected_filename: Option<&'static str>,
Expand All @@ -211,6 +222,7 @@ mod test {
pkg_dir,
has_v1: false,
has_alt_v1: false,
has_versionless: false,
expected_version: None,
expected_filename: None,
}
Expand All @@ -226,6 +238,11 @@ mod test {
self
}

pub const fn has_versionless(mut self) -> Self {
self.has_versionless = true;
self
}

pub const fn expects_v1(mut self) -> Self {
self.expected_version = Some(FoundConfig::V1);
self
Expand Down Expand Up @@ -257,9 +274,16 @@ mod test {
.with_filename(DEFAULT_MICROFRONTENDS_CONFIG_V1_ALT);

const LOAD_NONE: LoadDirTest = LoadDirTest::new("web");

const LOAD_VERSIONLESS: LoadDirTest = LoadDirTest::new("web")
.has_versionless()
.expects_v1()
.with_filename(DEFAULT_MICROFRONTENDS_CONFIG_V1);

#[test_case(LOAD_V1)]
#[test_case(LOAD_V1_ALT)]
#[test_case(LOAD_NONE)]
#[test_case(LOAD_VERSIONLESS)]
fn test_load_dir(case: LoadDirTest) {
let dir = TempDir::new().unwrap();
let repo_root = AbsoluteSystemPath::new(dir.path().to_str().unwrap()).unwrap();
Expand All @@ -271,6 +295,9 @@ mod test {
if case.has_alt_v1 {
add_v1_alt_config(&pkg_path).unwrap();
}
if case.has_versionless {
add_no_version_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 {
Expand Down
Loading