Skip to content

Commit

Permalink
Fix manifest template formatting, add tests for it (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptibell authored Jun 15, 2024
1 parent d4a985d commit 9387868
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ semver = { version = "1.0", features = ["serde"] }
tar = "0.4"
tempfile = "3.3"
thiserror = "1.0"
unindent = "0.2"
url = { version = "2.5", features = ["serde"] }
which = "6.0"
zip = "2.1"
Expand Down
7 changes: 3 additions & 4 deletions lib/manifests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

pub const MANIFEST_FILE_NAME: &str = "auth.toml";
const MANIFEST_DEFAULT_CONTENTS: &str = "\
pub(super) const MANIFEST_DEFAULT_CONTENTS: &str = "
# This file lists authentication tokens managed by Rokit, a toolchain manager for Roblox projects.
# For more information, see <|REPOSITORY_URL|>
Expand Down Expand Up @@ -196,10 +196,9 @@ impl ToString for AuthManifest {

impl Default for AuthManifest {
fn default() -> Self {
let document = MANIFEST_DEFAULT_CONTENTS
.replace("<|REPOSITORY_URL|>", env!("CARGO_PKG_REPOSITORY"))
let document = super::make_manifest_template(MANIFEST_DEFAULT_CONTENTS)
.parse::<DocumentMut>()
.unwrap();
.expect("default manifest template should be valid");
Self { document }
}
}
51 changes: 51 additions & 0 deletions lib/manifests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,54 @@ mod rokit;

pub use self::auth::{AuthManifest, MANIFEST_FILE_NAME as AUTH_MANIFEST_FILE_NAME};
pub use self::rokit::{RokitManifest, MANIFEST_FILE_NAME as ROKIT_MANIFEST_FILE_NAME};

/**
Helper function to make sure our authored manifest templates
have consistent formatting and the correct repository URL.
*/
fn make_manifest_template(template: &'static str) -> String {
let mut contents = unindent::unindent(template.trim())
.replace("<|REPOSITORY_URL|>", env!("CARGO_PKG_REPOSITORY"));
contents.push('\n');
contents
}

// Let's also test the formatting a bit to make sure nothing slips through :-)

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn has_no_indentation() {
let auth_contents = make_manifest_template(auth::MANIFEST_DEFAULT_CONTENTS);
let rokit_contents = make_manifest_template(rokit::MANIFEST_DEFAULT_CONTENTS);

assert!(!auth_contents.contains('\t'));
assert!(!rokit_contents.contains('\t'));

assert!(!auth_contents.contains("\n "));
assert!(!rokit_contents.contains("\n "));

assert!(!auth_contents.contains(" "));
assert!(!rokit_contents.contains(" "));
}

#[test]
fn ends_with_newline() {
assert!(make_manifest_template(auth::MANIFEST_DEFAULT_CONTENTS).ends_with('\n'));
assert!(make_manifest_template(rokit::MANIFEST_DEFAULT_CONTENTS).ends_with('\n'));
}

#[test]
fn contains_repo_url() {
let auth_contents = make_manifest_template(auth::MANIFEST_DEFAULT_CONTENTS);
let rokit_contents = make_manifest_template(rokit::MANIFEST_DEFAULT_CONTENTS);

assert!(auth_contents.contains(env!("CARGO_PKG_REPOSITORY")));
assert!(rokit_contents.contains(env!("CARGO_PKG_REPOSITORY")));

assert!(!auth_contents.contains("REPOSITORY_URL"));
assert!(!rokit_contents.contains("REPOSITORY_URL"));
}
}
8 changes: 4 additions & 4 deletions lib/manifests/rokit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ use crate::{
};

pub const MANIFEST_FILE_NAME: &str = "rokit.toml";
const MANIFEST_DEFAULT_CONTENTS: &str = "
pub(super) const MANIFEST_DEFAULT_CONTENTS: &str = "
# This file lists tools managed by Rokit, a toolchain manager for Roblox projects.
# For more information, see <|REPOSITORY_URL|>
# New tools can be added by running `rokit add <tool>` in a terminal.
[tools]
";

Expand Down Expand Up @@ -253,10 +254,9 @@ impl ToString for RokitManifest {

impl Default for RokitManifest {
fn default() -> Self {
let document = MANIFEST_DEFAULT_CONTENTS
.replace("<|REPOSITORY_URL|>", env!("CARGO_PKG_REPOSITORY"))
let document = super::make_manifest_template(MANIFEST_DEFAULT_CONTENTS)
.parse::<DocumentMut>()
.unwrap();
.expect("default manifest template should be valid");
Self { document }
}
}

0 comments on commit 9387868

Please sign in to comment.