summaryrefslogtreecommitdiff
path: root/cli/tools/registry/mod.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-07-22 15:46:37 -0400
committerGitHub <noreply@github.com>2024-07-22 15:46:37 -0400
commit6960637b5ce20f717d853ef42d6d8132297ab64b (patch)
tree7edb2e42401a863f35c36abc30226158e0deae38 /cli/tools/registry/mod.rs
parent92abdb7669d81b656ae0505cf923fca3b7feea01 (diff)
fix(publish): warn about missing license file (#24677)
Part of https://github.com/denoland/deno/issues/24676 , but just a warning for now.
Diffstat (limited to 'cli/tools/registry/mod.rs')
-rw-r--r--cli/tools/registry/mod.rs68
1 files changed, 68 insertions, 0 deletions
diff --git a/cli/tools/registry/mod.rs b/cli/tools/registry/mod.rs
index 2e781ccee..1c4e1bea7 100644
--- a/cli/tools/registry/mod.rs
+++ b/cli/tools/registry/mod.rs
@@ -462,6 +462,13 @@ impl PublishPreparer {
&publish_paths,
&diagnostics_collector,
);
+
+ if !has_license_file(publish_paths.iter().map(|p| &p.specifier)) {
+ diagnostics_collector.push(PublishDiagnostic::MissingLicense {
+ expected_path: root_dir.join("LICENSE"),
+ });
+ }
+
tar::create_gzipped_tarball(
&publish_paths,
LazyGraphSourceParser::new(&source_cache, &graph),
@@ -1187,6 +1194,36 @@ async fn check_if_git_repo_dirty(cwd: &Path) -> Option<String> {
}
}
+fn has_license_file<'a>(
+ mut specifiers: impl Iterator<Item = &'a ModuleSpecifier>,
+) -> bool {
+ let allowed_license_files = {
+ let files = HashSet::from([
+ "license",
+ "license.md",
+ "license.txt",
+ "licence",
+ "licence.md",
+ "licence.txt",
+ ]);
+ if cfg!(debug_assertions) {
+ for file in &files {
+ assert_eq!(*file, file.to_lowercase());
+ }
+ }
+ files
+ };
+ specifiers.any(|specifier| {
+ specifier
+ .path()
+ .rsplit_once('/')
+ .map(|(_, file)| {
+ allowed_license_files.contains(file.to_lowercase().as_str())
+ })
+ .unwrap_or(false)
+ })
+}
+
#[allow(clippy::print_stderr)]
fn ring_bell() {
// ASCII code for the bell character.
@@ -1195,6 +1232,10 @@ fn ring_bell() {
#[cfg(test)]
mod tests {
+ use deno_ast::ModuleSpecifier;
+
+ use crate::tools::registry::has_license_file;
+
use super::tar::PublishableTarball;
use super::tar::PublishableTarballFile;
use super::verify_version_manifest;
@@ -1296,4 +1337,31 @@ mod tests {
assert!(verify_version_manifest(meta_bytes, &package).is_err());
}
+
+ #[test]
+ fn test_has_license_files() {
+ fn has_license_file_str(expected: &[&str]) -> bool {
+ let specifiers = expected
+ .iter()
+ .map(|s| ModuleSpecifier::parse(s).unwrap())
+ .collect::<Vec<_>>();
+ has_license_file(specifiers.iter())
+ }
+
+ assert!(has_license_file_str(&["file:///LICENSE"]));
+ assert!(has_license_file_str(&["file:///license"]));
+ assert!(has_license_file_str(&["file:///LICENSE.txt"]));
+ assert!(has_license_file_str(&["file:///LICENSE.md"]));
+ assert!(has_license_file_str(&["file:///LICENCE"]));
+ assert!(has_license_file_str(&["file:///LICENCE.txt"]));
+ assert!(has_license_file_str(&["file:///LICENCE.md"]));
+ assert!(has_license_file_str(&[
+ "file:///other",
+ "file:///test/LICENCE.md"
+ ]),);
+ assert!(!has_license_file_str(&[
+ "file:///other",
+ "file:///test/tLICENSE"
+ ]),);
+ }
}