Skip to content

Commit 316e3e4

Browse files
refactor: move Browser and TestScope matching to methods
1 parent 805cd74 commit 316e3e4

1 file changed

Lines changed: 80 additions & 45 deletions

File tree

moz-webgpu-cts/src/shared.rs

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -449,17 +449,9 @@ impl<'a> TestPath<'a> {
449449
) -> Result<Self, ExecutionReportPathError<'a>> {
450450
let err = || ExecutionReportPathError { test_url_path };
451451

452-
let strip_prefix = |prefix, scope| {
453-
test_url_path
454-
.strip_prefix(prefix)
455-
.map(|stripped| (scope, stripped))
456-
};
457-
let (scope, path) = match browser {
458-
Browser::Firefox => strip_prefix("/_mozilla/", FirefoxTestScope::Mozilla.into())
459-
.or_else(|| strip_prefix("/", FirefoxTestScope::Upstream.into())),
460-
Browser::Servo => strip_prefix("/_webgpu/", ServoTestScope::WebGpu.into()),
461-
}
462-
.ok_or_else(err)?;
452+
let (scope, path) = browser
453+
.strip_scope_url_prefix(test_url_path)
454+
.ok_or_else(err)?;
463455

464456
if path.contains('\\') {
465457
return Err(err());
@@ -504,22 +496,9 @@ impl<'a> TestPath<'a> {
504496
.ok_or(err())?,
505497
);
506498

507-
let strip_prefix = |prefix, scope| {
508-
rel_meta_file_path
509-
.strip_prefix(prefix)
510-
.map(|stripped| (scope, stripped))
511-
};
512-
let (scope, path) = match browser {
513-
Browser::Firefox => {
514-
strip_prefix(SCOPE_DIR_FX_MOZILLA_STR, FirefoxTestScope::Mozilla.into()).or_else(
515-
|_| strip_prefix(SCOPE_DIR_FX_UPSTREAM_STR, FirefoxTestScope::Upstream.into()),
516-
)
517-
}
518-
Browser::Servo => {
519-
strip_prefix(SCOPE_DIR_SERVO_WEBGPU_STR, ServoTestScope::WebGpu.into())
520-
}
521-
}
522-
.map_err(|_e| err())?;
499+
let (scope, path) = browser
500+
.strip_scope_metadata_parent_path(rel_meta_file_path)
501+
.map_err(|_e| err())?;
523502

524503
let Ok(path) = path.strip_prefix("meta/") else {
525504
return Err(err());
@@ -586,14 +565,12 @@ impl<'a> TestPath<'a> {
586565
scope,
587566
} = self;
588567
lazy_format!(move |f| {
589-
let scope_prefix = match scope {
590-
TestScope::Firefox(scope) => match scope {
591-
FirefoxTestScope::Upstream => "",
592-
FirefoxTestScope::Mozilla => "_mozilla/",
593-
},
594-
TestScope::Servo(ServoTestScope::WebGpu) => "_webgpu/",
595-
};
596-
write!(f, "{scope_prefix}{}", path.components().join_with('/'))?;
568+
write!(
569+
f,
570+
"{}{}",
571+
scope.url_prefix(),
572+
path.components().join_with('/')
573+
)?;
597574
if let Some(variant) = variant.as_ref() {
598575
write!(f, "{}", variant)?;
599576
}
@@ -608,16 +585,10 @@ impl<'a> TestPath<'a> {
608585
scope,
609586
} = self;
610587

611-
let scope_dir = match scope {
612-
TestScope::Firefox(scope) => match scope {
613-
FirefoxTestScope::Upstream => SCOPE_DIR_FX_UPSTREAM_COMPONENTS,
614-
FirefoxTestScope::Mozilla => SCOPE_DIR_FX_MOZILLA_COMPONENTS,
615-
},
616-
TestScope::Servo(ServoTestScope::WebGpu) => SCOPE_DIR_SERVO_WEBGPU_COMPONENTS,
617-
}
618-
.iter()
619-
.chain(&["meta"])
620-
.join_with(std::path::MAIN_SEPARATOR);
588+
let scope_dir = scope
589+
.metadata_parent_path_components()
590+
.chain(["meta"].iter().cloned())
591+
.join_with(std::path::MAIN_SEPARATOR);
621592

622593
lazy_format!(move |f| { write!(f, "{scope_dir}{}{path}.ini", std::path::MAIN_SEPARATOR) })
623594
}
@@ -669,6 +640,44 @@ pub(crate) enum Browser {
669640
Servo,
670641
}
671642

643+
impl Browser {
644+
/// NOTE: Keep this implementation in sync with [`TestScope::url_prefix`].
645+
pub(crate) fn strip_scope_url_prefix<'a>(
646+
&self,
647+
url_path: &'a str,
648+
) -> Option<(TestScope, &'a str)> {
649+
let strip_prefix = |prefix, scope| {
650+
url_path
651+
.strip_prefix(prefix)
652+
.map(|stripped| (scope, stripped))
653+
};
654+
match self {
655+
Browser::Firefox => strip_prefix("/_mozilla/", FirefoxTestScope::Mozilla.into())
656+
.or_else(|| strip_prefix("/", FirefoxTestScope::Upstream.into())),
657+
Browser::Servo => strip_prefix("/_webgpu/", ServoTestScope::WebGpu.into()),
658+
}
659+
}
660+
661+
/// NOTE: Keep this implementation in sync with [`TestScope::metadata_parent_path_components`].
662+
pub(crate) fn strip_scope_metadata_parent_path<'a>(
663+
&self,
664+
path: &'a Utf8Path,
665+
) -> Result<(TestScope, &'a Utf8Path), std::path::StripPrefixError> {
666+
let strip_prefix =
667+
|prefix, scope| path.strip_prefix(prefix).map(|stripped| (scope, stripped));
668+
match self {
669+
Browser::Firefox => {
670+
strip_prefix(SCOPE_DIR_FX_MOZILLA_STR, FirefoxTestScope::Mozilla.into()).or_else(
671+
|_| strip_prefix(SCOPE_DIR_FX_UPSTREAM_STR, FirefoxTestScope::Upstream.into()),
672+
)
673+
}
674+
Browser::Servo => {
675+
strip_prefix(SCOPE_DIR_SERVO_WEBGPU_STR, ServoTestScope::WebGpu.into())
676+
}
677+
}
678+
}
679+
}
680+
672681
/// Symbolically represents a file root from which tests and metadata are based. Scopes are based
673682
/// on a specific [`Browser`].
674683
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
@@ -677,6 +686,32 @@ pub(crate) enum TestScope {
677686
Servo(ServoTestScope),
678687
}
679688

689+
impl TestScope {
690+
/// NOTE: Keep this implementation in sync with [`Browser::strip_scope_url_prefix`].
691+
fn url_prefix(&self) -> &str {
692+
match self {
693+
TestScope::Firefox(scope) => match scope {
694+
FirefoxTestScope::Upstream => "",
695+
FirefoxTestScope::Mozilla => "_mozilla/",
696+
},
697+
TestScope::Servo(ServoTestScope::WebGpu) => "_webgpu/",
698+
}
699+
}
700+
701+
/// NOTE: Keep this implementation in sync with [`Browser::strip_scope_metadata_parent_path`].
702+
fn metadata_parent_path_components(&self) -> impl Iterator<Item = &str> + Clone {
703+
match self {
704+
TestScope::Firefox(scope) => match scope {
705+
FirefoxTestScope::Upstream => SCOPE_DIR_FX_UPSTREAM_COMPONENTS,
706+
FirefoxTestScope::Mozilla => SCOPE_DIR_FX_MOZILLA_COMPONENTS,
707+
},
708+
TestScope::Servo(ServoTestScope::WebGpu) => SCOPE_DIR_SERVO_WEBGPU_COMPONENTS,
709+
}
710+
.iter()
711+
.cloned()
712+
}
713+
}
714+
680715
/// Subset of [`TestScope`] for [`Browser::Firefox`].
681716
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
682717
pub(crate) enum FirefoxTestScope {

0 commit comments

Comments
 (0)