Skip to content

Commit

Permalink
fix(mfe): properly handle versionless configuration (vercel#9945)
Browse files Browse the repository at this point in the history
### Description

TSIA

We were requiring the version to exist in `VersionOnly` schema so we
could decide which config schema to use.

I'm assuming I broke this in
vercel#9883, but it's not immediately
obvious to me how.

### Testing Instructions

Added failing unit test for versionless `Config::load_from_dir` call
  • Loading branch information
chris-olszewski authored and joshnuss committed Feb 15, 2025
1 parent ce8fd45 commit 37ba98b
Showing 1 changed file with 30 additions and 3 deletions.
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

0 comments on commit 37ba98b

Please sign in to comment.