Skip to content

Commit

Permalink
adding all violations to packaage.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
perryqh committed Jul 31, 2024
1 parent 1301240 commit b24082b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[package]
name = "pks"
version = "0.2.16"
version = "0.2.17"
edition = "2021"
description = "Welcome! Please see https://github.com/rubyatscale/pks for more information!"
license = "MIT"
Expand Down
38 changes: 23 additions & 15 deletions src/packs/creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,41 @@ pub enum CreateResult {
AlreadyExists,
}

pub const NEW_PACKAGE_YML_CONTENTS: &str = "enforce_dependencies: true
enforce_privacy: true
enforce_layers: true";

pub fn create(
configuration: &Configuration,
name: &str,
) -> anyhow::Result<CreateResult> {
let existing_pack = configuration.pack_set.for_pack(&name);
let existing_pack = configuration.pack_set.for_pack(name);
if existing_pack.is_ok() {
println!("`{}` already exists!", &name);
return Ok(CreateResult::AlreadyExists);
}
let new_pack_path =
configuration.absolute_root.join(&name).join("package.yml");

let new_pack_path = configuration.absolute_root.join(name);

let new_pack = Pack::from_contents(
&new_pack_path,
&new_pack_path.join("package.yml"),
&configuration.absolute_root,
"enforce_dependencies: true",
NEW_PACKAGE_YML_CONTENTS,
PackageTodo::default(),
)?;

write_pack_to_disk(&new_pack)?;
std::fs::create_dir_all(new_pack_path.join("app/public/"))
.context("failed to create app/public")?;

let readme = readme(name);
let readme_path = &new_pack_path.join("README.md");
std::fs::write(readme_path, readme).context("Failed to write README.md")?;

let readme = format!(
Ok(CreateResult::Success)
}

fn readme(pack_name: &str) -> String {
format!(
"Welcome to `{}`!
If you're the author, please consider replacing this file with a README.md, which may contain:
Expand All @@ -48,12 +61,7 @@ If you're the author, please consider replacing this file with a README.md, whic
README.md should change as your public API changes.
See https://github.com/rubyatscale/packs#readme for more info!",
new_pack.name
);

let readme_path = configuration.absolute_root.join(&name).join("README.md");
std::fs::write(readme_path, readme).context("Failed to write README.md")?;

Ok(CreateResult::Success)
See https://github.com/rubyatscale/pks#readme for more info!",
pack_name
)
}
24 changes: 8 additions & 16 deletions tests/create_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use assert_cmd::Command;
use predicates::prelude::*;
use pretty_assertions::assert_eq;
use std::{error::Error, fs};
use std::{error::Error, fs, path::Path};

mod common;

Expand All @@ -18,11 +18,15 @@ fn test_create() -> Result<(), Box<dyn Error>> {
"Successfully created `packs/foobar`!",
));

let expected = "enforce_dependencies: true\n";
let actual = fs::read_to_string(
"tests/fixtures/simple_app/packs/foobar/package.yml",
).unwrap_or_else(|_| panic!("Could not read file tests/fixtures/simple_app/packs/foobar/package.yml"));
assert_eq!(expected, actual);
assert!(actual.contains("enforce_dependencies: true"));
assert!(actual.contains("enforce_privacy: true"));
assert!(actual.contains("enforce_layers: true"));
assert!(
Path::new("tests/fixtures/simple_app/packs/foobar/app/public").exists()
);

let expected_readme = String::from("\
Welcome to `packs/foobar`!
Expand All @@ -39,7 +43,7 @@ If you're the author, please consider replacing this file with a README.md, whic
README.md should change as your public API changes.
See https://github.com/rubyatscale/packs#readme for more info!");
See https://github.com/rubyatscale/pks#readme for more info!");

let actual_readme =
fs::read_to_string("tests/fixtures/simple_app/packs/foobar/README.md").unwrap_or_else(|e| {
Expand All @@ -65,18 +69,6 @@ fn test_create_already_exists() -> Result<(), Box<dyn Error>> {
.success()
.stdout(predicate::str::contains("`packs/foo` already exists!"));

let expected = String::from(
"\
enforce_dependencies: true
enforce_privacy: true
dependencies:
- packs/baz
",
);

let actual =
fs::read_to_string("tests/fixtures/simple_app/packs/foo/package.yml")?;
assert_eq!(expected, actual);
common::teardown();
Ok(())
}

0 comments on commit b24082b

Please sign in to comment.