🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix upper camle
  • Loading branch information
levkk committed Sep 3, 2023
commit 4896ecc10adb61032b4f3a47341294a440169a65
12 changes: 11 additions & 1 deletion pgml-apps/cargo-pgml-components/src/frontend/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ pub fn add(path: &Path, overwrite: bool) {
exit(1);
}

let path = path
.components()
.map(|c| {
c.as_os_str()
.to_str()
.expect("utf-8 component")
.to_case(Case::Snake)
})
.collect::<PathBuf>();

let mut parent = path.parent().expect("paths should have parents");
let mut full_path = Path::new(COMPONENT_DIRECTORY).join(parent);

Expand All @@ -123,7 +133,7 @@ pub fn add(path: &Path, overwrite: bool) {
full_path = Path::new(COMPONENT_DIRECTORY).join(parent);
}

let component = Component::from(path);
let component = Component::from(path.as_path());
let path = component.full_path();

if path.exists() && !overwrite {
Expand Down
111 changes: 111 additions & 0 deletions pgml-apps/cargo-pgml-components/tests/test_add_component.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use assert_cmd::Command;
use assert_fs::prelude::*;
use predicates::prelude::*;
use std::fs::read_to_string;

#[test]
fn test_help() {
Expand Down Expand Up @@ -39,6 +40,93 @@ fn test_add_component() {
}
}

#[test]
fn test_add_upper_camel() {
let temp = assert_fs::TempDir::new().unwrap();
let mut cmd = Command::cargo_bin("cargo-pgml-components").unwrap();
cmd.arg("pgml-components")
.arg("--project-path")
.arg(temp.path().display().to_string())
.arg("add")
.arg("component")
.arg("TestComponent");

cmd.assert().success();

for path in [
"mod.rs",
"template.html",
"test_component.scss",
"test_component_controller.js",
] {
temp.child(&format!("src/components/test_component/{}", path))
.assert(predicate::path::exists());
}

let rust = read_to_string(temp.child("src/components/test_component/mod.rs").path()).unwrap();
assert!(rust.contains("pub struct TestComponent {"));

let js = read_to_string(
temp.child("src/components/test_component/test_component_controller.js")
.path(),
)
.unwrap();
assert!(js.contains("export default class extends Controller"));
assert!(js.contains("console.log('Initialized test-component')"));

let html = read_to_string(
temp.child("src/components/test_component/template.html")
.path(),
)
.unwrap();
assert!(html.contains("<div data-controller=\"test-component\">"));

let mut cmd = Command::cargo_bin("cargo-pgml-components").unwrap();
cmd.arg("pgml-components")
.arg("--project-path")
.arg(temp.path().display().to_string())
.arg("add")
.arg("component")
.arg("RandomTest/Hello/snake_path/CamelComponent");
cmd.assert().success();

for path in [
"mod.rs",
"template.html",
"camel_component.scss",
"camel_component_controller.js",
] {
temp.child(&format!(
"src/components/random_test/hello/snake_path/camel_component/{}",
path
))
.assert(predicate::path::exists());
}

let js = temp.child(
"src/components/random_test/hello/snake_path/camel_component/camel_component_controller.js",
);

let js = read_to_string(js.path()).unwrap();
assert!(js.contains("export default class extends Controller"));
assert!(js.contains("console.log('Initialized random-test-hello-snake-path-camel-component')"));

let html = read_to_string(
temp.child("src/components/random_test/hello/snake_path/camel_component/template.html")
.path(),
)
.unwrap();
assert!(html.contains("<div data-controller=\"random-test-hello-snake-path-camel-component\">"));

let rust = read_to_string(
temp.child("src/components/random_test/hello/snake_path/camel_component/mod.rs")
.path(),
)
.unwrap();
assert!(rust.contains("pub struct CamelComponent {"));
assert!(rust.contains("impl CamelComponent {"));
}

#[test]
fn test_add_subcomponent() {
let mut cmd = Command::cargo_bin("cargo-pgml-components").unwrap();
Expand Down Expand Up @@ -97,3 +185,26 @@ fn test_add_subcomponent() {
"component cannot be placed into a directory that has a component already",
));
}

#[test]
fn test_invalid_component_names() {
let temp = assert_fs::TempDir::new().unwrap();
for name in [
"has-a-dash",
"5_starts_with_a_number",
"has%_special_characters",
] {
let mut cmd = Command::cargo_bin("cargo-pgml-components").unwrap();

cmd.arg("pgml-components")
.arg("--project-path")
.arg(temp.path().display().to_string())
.arg("add")
.arg("component")
.arg(name);

cmd.assert()
.failure()
.stdout(predicate::str::contains("component name is not valid"));
}
}