Skip to content

Commit

Permalink
refactor: move Browser and TestScope matching to methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ErichDonGubler committed May 10, 2024
1 parent b448f18 commit 8b5d3e0
Showing 1 changed file with 80 additions and 45 deletions.
125 changes: 80 additions & 45 deletions moz-webgpu-cts/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,9 @@ impl<'a> TestPath<'a> {
) -> Result<Self, ExecutionReportPathError<'a>> {
let err = || ExecutionReportPathError { test_url_path };

let strip_prefix = |prefix, scope| {
test_url_path
.strip_prefix(prefix)
.map(|stripped| (scope, stripped))
};
let (scope, path) = match browser {
Browser::Firefox => strip_prefix("/_mozilla/", FirefoxTestScope::Mozilla.into())
.or_else(|| strip_prefix("/", FirefoxTestScope::Upstream.into())),
Browser::Servo => strip_prefix("/_webgpu/", ServoTestScope::WebGpu.into()),
}
.ok_or_else(err)?;
let (scope, path) = browser
.strip_scope_url_prefix(test_url_path)
.ok_or_else(err)?;

if path.contains('\\') {
return Err(err());
Expand Down Expand Up @@ -503,22 +495,9 @@ impl<'a> TestPath<'a> {
.ok_or(err())?,
);

let strip_prefix = |prefix, scope| {
rel_meta_file_path
.strip_prefix(prefix)
.map(|stripped| (scope, stripped))
};
let (scope, path) = match browser {
Browser::Firefox => {
strip_prefix(SCOPE_DIR_FX_MOZILLA_STR, FirefoxTestScope::Mozilla.into()).or_else(
|_| strip_prefix(SCOPE_DIR_FX_UPSTREAM_STR, FirefoxTestScope::Upstream.into()),
)
}
Browser::Servo => {
strip_prefix(SCOPE_DIR_SERVO_WEBGPU_STR, ServoTestScope::WebGpu.into())
}
}
.map_err(|_e| err())?;
let (scope, path) = browser
.strip_scope_metadata_parent_path(rel_meta_file_path)
.map_err(|_e| err())?;

let Ok(path) = path.strip_prefix("meta/") else {
return Err(err());
Expand Down Expand Up @@ -585,14 +564,12 @@ impl<'a> TestPath<'a> {
scope,
} = self;
lazy_format!(move |f| {
let scope_prefix = match scope {
TestScope::Firefox(scope) => match scope {
FirefoxTestScope::Upstream => "",
FirefoxTestScope::Mozilla => "_mozilla/",
},
TestScope::Servo(ServoTestScope::WebGpu) => "_webgpu/",
};
write!(f, "{scope_prefix}{}", path.components().join_with('/'))?;
write!(
f,
"{}{}",
scope.url_prefix(),
path.components().join_with('/')
)?;
if let Some(variant) = variant.as_ref() {
write!(f, "{}", variant)?;
}
Expand All @@ -607,16 +584,10 @@ impl<'a> TestPath<'a> {
scope,
} = self;

let scope_dir = match scope {
TestScope::Firefox(scope) => match scope {
FirefoxTestScope::Upstream => SCOPE_DIR_FX_UPSTREAM_COMPONENTS,
FirefoxTestScope::Mozilla => SCOPE_DIR_FX_MOZILLA_COMPONENTS,
},
TestScope::Servo(ServoTestScope::WebGpu) => SCOPE_DIR_SERVO_WEBGPU_COMPONENTS,
}
.iter()
.chain(&["meta"])
.join_with(std::path::MAIN_SEPARATOR);
let scope_dir = scope
.metadata_parent_path_components()
.chain(["meta"].iter().cloned())
.join_with(std::path::MAIN_SEPARATOR);

lazy_format!(move |f| { write!(f, "{scope_dir}{}{path}.ini", std::path::MAIN_SEPARATOR) })
}
Expand Down Expand Up @@ -668,6 +639,44 @@ pub(crate) enum Browser {
Servo,
}

impl Browser {
/// NOTE: Keep this implementation in sync with [`TestScope::url_prefix`].
pub(crate) fn strip_scope_url_prefix<'a>(
&self,
url_path: &'a str,
) -> Option<(TestScope, &'a str)> {
let strip_prefix = |prefix, scope| {
url_path
.strip_prefix(prefix)
.map(|stripped| (scope, stripped))
};
match self {
Browser::Firefox => strip_prefix("/_mozilla/", FirefoxTestScope::Mozilla.into())
.or_else(|| strip_prefix("/", FirefoxTestScope::Upstream.into())),
Browser::Servo => strip_prefix("/_webgpu/", ServoTestScope::WebGpu.into()),
}
}

/// NOTE: Keep this implementation in sync with [`TestScope::metadata_parent_path_components`].
pub(crate) fn strip_scope_metadata_parent_path<'a>(
&self,
path: &'a Utf8Path,
) -> Result<(TestScope, &'a Utf8Path), std::path::StripPrefixError> {
let strip_prefix =
|prefix, scope| path.strip_prefix(prefix).map(|stripped| (scope, stripped));
match self {
Browser::Firefox => {
strip_prefix(SCOPE_DIR_FX_MOZILLA_STR, FirefoxTestScope::Mozilla.into()).or_else(
|_| strip_prefix(SCOPE_DIR_FX_UPSTREAM_STR, FirefoxTestScope::Upstream.into()),
)
}
Browser::Servo => {
strip_prefix(SCOPE_DIR_SERVO_WEBGPU_STR, ServoTestScope::WebGpu.into())
}
}
}
}

/// Symbolically represents a file root from which tests and metadata are based. Scopes are based
/// on a specific [`Browser`].
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
Expand All @@ -676,6 +685,32 @@ pub(crate) enum TestScope {
Servo(ServoTestScope),
}

impl TestScope {
/// NOTE: Keep this implementation in sync with [`Browser::strip_scope_url_prefix`].
fn url_prefix(&self) -> &str {
match self {
TestScope::Firefox(scope) => match scope {
FirefoxTestScope::Upstream => "",
FirefoxTestScope::Mozilla => "_mozilla/",
},
TestScope::Servo(ServoTestScope::WebGpu) => "_webgpu/",
}
}

/// NOTE: Keep this implementation in sync with [`Browser::strip_scope_metadata_parent_path`].
fn metadata_parent_path_components(&self) -> impl Iterator<Item = &str> + Clone {
match self {
TestScope::Firefox(scope) => match scope {
FirefoxTestScope::Upstream => SCOPE_DIR_FX_UPSTREAM_COMPONENTS,
FirefoxTestScope::Mozilla => SCOPE_DIR_FX_MOZILLA_COMPONENTS,
},
TestScope::Servo(ServoTestScope::WebGpu) => SCOPE_DIR_SERVO_WEBGPU_COMPONENTS,
}
.iter()
.cloned()
}
}

/// Subset of [`TestScope`] for [`Browser::Firefox`].
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) enum FirefoxTestScope {
Expand Down

0 comments on commit 8b5d3e0

Please sign in to comment.