Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ that directory to see how it should look like.
- Make each tool description as precise as possible. Please limit the
description to **500 characters**.
- Add a license. If it's a proprietary tool, use `license: proprietary`.
- Add at least one category: `linter`, `formatter`, `performance`, or `meta`.
- Add at least one tag, and include as many relevant tags as possible. Choose from
`data/tags.yml`. If a tool does not match any existing tag, feel free to add a
new tag but also add it to `data/tags.yml`.
Expand Down
2 changes: 1 addition & 1 deletion ci/crates/render/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ mod tests {

#[test]
fn cached_deprecation_never_overrides_explicit_markers() -> Result<()> {
let fixture = r#"{"name":"Example Tool","categories":[],"tags":[{"name":"Rust","value":"rust","tag_type":"language"}],"license":"MIT","types":[],"homepage":"https://example.com","description":"Example"}"#;
let fixture = r#"{"name":"Example Tool","categories":["linter"],"tags":[{"name":"Rust","value":"rust","tag_type":"language"}],"license":"MIT","types":[],"homepage":"https://example.com","description":"Example"}"#;
for cached_marker in [Some(true), Some(false), None] {
let cached = BTreeMap::from([(
"example-tool".into(),
Expand Down
2 changes: 1 addition & 1 deletion ci/crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mod tests {
fn entry(tags: &[Tag]) -> Result<Entry> {
Ok(Entry {
name: String::from("Multi Tool").try_into()?,
categories: BTreeSet::new(),
categories: BTreeSet::from([crate::types::Category::Linter]).try_into()?,
tags: tags.iter().cloned().collect::<BTreeSet<_>>().try_into()?,
license: "MIT".into(),
types: BTreeSet::new(),
Expand Down
8 changes: 4 additions & 4 deletions ci/crates/render/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize, de::value::StrDeserializer};
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};

pub use crate::validated::{EntryName, EntryTags};
pub use crate::validated::{EntryCategories, EntryName, EntryTags};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub enum Type {
Expand Down Expand Up @@ -62,7 +62,7 @@ pub enum Category {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct ParsedEntry {
pub name: EntryName,
pub categories: BTreeSet<Category>,
pub categories: EntryCategories,
pub tags: EntryTags,
pub license: String,
pub types: BTreeSet<String>,
Expand Down Expand Up @@ -94,7 +94,7 @@ pub enum ToolType {
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Entry {
pub name: EntryName,
pub categories: BTreeSet<Category>,
pub categories: EntryCategories,
pub tags: EntryTags<Tag>,
pub license: String,
pub types: BTreeSet<ToolType>,
Expand Down Expand Up @@ -259,7 +259,7 @@ impl Catalog {
pub struct ApiEntry {
/// The original entry name (not slugified)
pub name: String,
pub categories: BTreeSet<Category>,
pub categories: EntryCategories,
pub languages: Vec<String>,
pub other: Vec<String>,
pub licenses: Vec<String>,
Expand Down
47 changes: 47 additions & 0 deletions ci/crates/render/src/validated.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::types::Category;
use anyhow::{Result, ensure};
use serde::{Deserialize, Serialize};
use std::collections::{BTreeSet, btree_set};
Expand Down Expand Up @@ -89,6 +90,23 @@ impl<'a, T: Ord> IntoIterator for &'a EntryTags<T> {
}
}

/// A nonempty, sorted set of tool categories, with no mutable access to its contents.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(try_from = "BTreeSet<Category>")]
pub struct EntryCategories(BTreeSet<Category>);

impl TryFrom<BTreeSet<Category>> for EntryCategories {
type Error = anyhow::Error;

fn try_from(categories: BTreeSet<Category>) -> Result<Self> {
ensure!(
!categories.is_empty(),
"Entry must have at least one category: linter, formatter, performance, or meta."
);
Ok(Self(categories))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -125,6 +143,35 @@ mod tests {
Ok(())
}

#[test]
fn categories_reject_empty_sets_in_constructors_and_serde() {
assert!(EntryCategories::try_from(BTreeSet::new()).is_err());
for input in ["[]", "null", "\"linter\"", "[\"unknown\"]"] {
assert!(serde_json::from_str::<EntryCategories>(input).is_err());
assert!(serde_saphyr::from_str::<EntryCategories>(input).is_err());
}
}

#[test]
fn categories_are_sorted_and_deduplicated_through_serde() -> Result<()> {
let categories =
EntryCategories::try_from(BTreeSet::from([Category::Formatter, Category::Linter]))?;
let encoded = r#"["formatter","linter","formatter"]"#;
assert_eq!(
serde_json::from_str::<EntryCategories>(encoded)?,
categories
);
assert_eq!(
serde_saphyr::from_str::<EntryCategories>(encoded)?,
categories
);
assert_eq!(
serde_json::to_value(categories)?,
json!(["linter", "formatter"])
);
Ok(())
}

#[test]
fn tags_reject_empty_sets_in_constructors_and_serde() {
assert!(EntryTags::<String>::try_from(BTreeSet::new()).is_err());
Expand Down
50 changes: 50 additions & 0 deletions ci/crates/render/tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,56 @@ fn full_catalog_preserves_original_sources_through_api_output() -> Result<()> {
Ok(())
}

#[test]
fn entries_require_nonempty_categories_in_yaml_and_json() -> Result<()> {
let parsed = serde_json::to_value(parsed()?)?;
let entry = Entry::from_parsed(self::parsed()?, &[tag("rust", Type::Language)])?;
let normalized = serde_json::to_value(&entry)?;
let api = create_api(vec![entry], &[tag("rust", Type::Language)], &[]);
let api = serde_json::to_value(api.values().next().context("Expected API entry")?)?;

for categories in [
None,
Some(json!([])),
Some(json!(null)),
Some(json!(["unknown"])),
] {
for (kind, mut value) in [
("parsed", parsed.clone()),
("normalized", normalized.clone()),
("api", api.clone()),
] {
if let Some(categories) = &categories {
value["categories"] = categories.clone();
} else {
value
.as_object_mut()
.context("Expected object")?
.remove("categories");
}
let encoded = serde_json::to_string(&value)?;
let (json_error, yaml_error) = match kind {
"parsed" => (
serde_json::from_str::<ParsedEntry>(&encoded).is_err(),
serde_saphyr::from_str::<ParsedEntry>(&encoded).is_err(),
),
"normalized" => (
serde_json::from_str::<Entry>(&encoded).is_err(),
serde_saphyr::from_str::<Entry>(&encoded).is_err(),
),
_ => (
serde_json::from_str::<ApiEntry>(&encoded).is_err(),
serde_saphyr::from_str::<ApiEntry>(&encoded).is_err(),
),
};
assert!(json_error && yaml_error, "{kind}: {encoded}");
}
}
let yaml = "name: Example\ncategories:\ntags: [rust]\nlicense: MIT\ntypes: [cli]\nhomepage: https://example.com\ndescription: Example\n";
assert!(serde_saphyr::from_str::<ParsedEntry>(yaml).is_err());
Ok(())
}

#[test]
fn normalization_preserves_fields_and_uses_the_first_matching_tag() -> Result<()> {
let original = parsed()?;
Expand Down
3 changes: 2 additions & 1 deletion data/tools/pixee.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Pixee
categories: []
categories:
- linter
tags:
- java
- python
Expand Down
Loading