Skip to content

Commit 40d05fc

Browse files
refactor: move Browser and TestScope matching to methods
1 parent dad3080 commit 40d05fc

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
@@ -448,17 +448,9 @@ impl<'a> TestPath<'a> {
448448
) -> Result<Self, ExecutionReportPathError<'a>> {
449449
let err = || ExecutionReportPathError { test_url_path };
450450

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

463455
if path.contains('\\') {
464456
return Err(err());
@@ -503,22 +495,9 @@ impl<'a> TestPath<'a> {
503495
.ok_or(err())?,
504496
);
505497

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

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

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

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

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

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

0 commit comments

Comments
 (0)