Skip to content

Commit

Permalink
fix: make all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr committed Jul 20, 2023
1 parent a08dc44 commit b2d0cfc
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 131 deletions.
10 changes: 5 additions & 5 deletions src/broken_links_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,19 @@ mod test {
let config = config(Some(&indoc! {"
---
title: Not Interesting
base_path: /not_docs
base_path: /not_docs/
"}));

let root = vec![
page_with_base_path(
"README.md",
"Getting Started",
"[I'm on a](/nested/)\n[highway to hell](/nested/other.html)",
"/not_docs",
"/not_docs/",
),
page_with_base_path("other.md", "Getting Started", "No links!", "/not_docs"),
page_with_base_path("nested/README.md", "Nested", "Content", "/not_docs"),
page_with_base_path("nested/other.md", "Nested Child", "No links!", "/not_docs"),
page_with_base_path("other.md", "Getting Started", "No links!", "/not_docs/"),
page_with_base_path("nested/README.md", "Nested", "Content", "/not_docs/"),
page_with_base_path("nested/other.md", "Nested Child", "No links!", "/not_docs/"),
];
let mut site = Site::in_memory(config.clone());
site.build(config.clone(), &root).unwrap();
Expand Down
22 changes: 6 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ impl DocgenYaml {
}

if !path.ends_with("/") {
path.push('/');
return Err(Error::new(format!(
"Base path must end with /. Got `{}`.",
path
)));
}
}

Expand Down Expand Up @@ -201,7 +204,7 @@ impl NavRule {
fn from_yaml_input(input: Vec<Navigation>) -> Vec<NavRule> {
let mut rules = vec![];
for item in input {
if item.children.is_some() {
if item.children.is_some() || item.path.is_dir() {
let dir_rules = Self::build_directory_rules(&item);
rules.push(dir_rules);
} else {
Expand Down Expand Up @@ -482,19 +485,6 @@ mod test {
);
}

#[test]
fn validate_base_path_ends_with_slash() {
let yaml = indoc! {"
---
title: The Title
base_path: /docs
"};

let config = Config::from_yaml_str(Path::new(""), yaml, false).unwrap();

assert_eq!(config.base_path(), "/docs/");
}

#[test]
fn validate_default_base_path() {
let yaml = indoc! {"
Expand All @@ -513,7 +503,7 @@ mod test {
---
title: The Title
navigation:
- path: docs/tutorial.md
- path: tutorial.md
children: not-wildcard
"};

Expand Down
14 changes: 12 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,23 @@ impl Document {
} else {
path.with_extension("html")
};
let uri_path = format!("{}{}", base_path, Link::path_to_uri(&html_path));

let mut path_elements = uri_path.split('/').collect::<Vec<_>>();
let mut uri_path = format!("{}{}", base_path, Link::path_to_uri(&html_path));
if is_root && !uri_path.ends_with("/") {
uri_path.push_str("/");
}

let mut path_elements = uri_path
.trim_end_matches("/")
.split('/')
.collect::<Vec<_>>();

path_elements.pop();
let parent = if path_elements.len() == 1 {
Path::new("/").to_path_buf()
} else {
// parent path must always end with /
path_elements.push("");
Path::new(&path_elements.join("/")).to_path_buf()
};

Expand Down
32 changes: 15 additions & 17 deletions src/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ impl<'a> Navigation<'a> {
directories.insert(String::from(base_path), vec![]);

for doc in docs {
if doc.src() == "" {
continue;
}

let uri_path = &doc.uri_path;
let html_path = &doc.html_path;
let title = &doc.title;
Expand Down Expand Up @@ -71,8 +75,7 @@ impl<'a> Navigation<'a> {
}
}

let mut links = directories.remove(&String::from(base_path)).unwrap();
links
directories.remove(&String::from(base_path)).unwrap()
}

/// Customizes the navigation tree given some rules provided through the
Expand Down Expand Up @@ -390,8 +393,8 @@ mod test {
docs.par_sort_by(document_sort);

let rules = vec![
NavRule::File(PathBuf::from("docs/one.md")),
NavRule::Dir(PathBuf::from("docs/child"), Some(DirIncludeRule::WildCard)),
NavRule::File(PathBuf::from("one.md")),
NavRule::Dir(PathBuf::from("child"), Some(DirIncludeRule::WildCard)),
];

insta::with_settings!({
Expand Down Expand Up @@ -420,16 +423,13 @@ mod test {
docs.par_sort_by(document_sort);

let rules = vec![
NavRule::File(PathBuf::from("docs").join("one.md")),
NavRule::File(PathBuf::from("one.md")),
NavRule::Dir(
PathBuf::from("docs").join("child"),
PathBuf::from("child"),
Some(DirIncludeRule::Explicit(vec![NavRule::Dir(
PathBuf::from("docs").join("child").join("nested"),
PathBuf::from("child").join("nested"),
Some(DirIncludeRule::Explicit(vec![NavRule::File(
PathBuf::from("docs")
.join("child")
.join("nested")
.join("four.md"),
PathBuf::from("child").join("nested").join("four.md"),
)])),
)])),
),
Expand All @@ -456,9 +456,7 @@ mod test {
];
docs.par_sort_by(document_sort);

let rules = vec![NavRule::File(
PathBuf::from("docs").join("child").join("three.md"),
)];
let rules = vec![NavRule::File(PathBuf::from("child").join("three.md"))];

insta::with_settings!({
description => "Manual menu file from nested directory",
Expand All @@ -482,9 +480,9 @@ mod test {
docs.par_sort_by(document_sort);

let rules = vec![NavRule::Dir(
PathBuf::from("docs").join("child"),
PathBuf::from("child"),
Some(DirIncludeRule::Explicit(vec![NavRule::File(
PathBuf::from("docs").join("one.md"),
PathBuf::from("one.md"),
)])),
)];

Expand All @@ -505,7 +503,7 @@ mod test {
let config = config(Some(indoc! {"
---
title: Not in the root
base_path: /example
base_path: /example/
"}));

let mut docs = vec![
Expand Down
48 changes: 24 additions & 24 deletions src/snapshots/docgen__navigation__test__basic_navigation.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,71 @@ description: Basic navigation
---
[
Link {
src: "1.one.md",
children: [],
path: "/1.one",
title: "One",
children: [],
index: 4294967295,
},
Link {
src: "2.two.md",
children: [],
path: "/2.two",
title: "Two",
children: [],
index: 4294967295,
},
Link {
src: "4.four.md",
children: [],
path: "/4.four",
title: "Four",
children: [],
index: 4294967295,
},
Link {
path: "/child/",
title: "Nested Root",
src: "child",
children: [
Link {
src: "child/3.three.md",
children: [],
path: "/child/3.three",
title: "Three",
children: [],
index: 4294967295,
},
Link {
src: "child/5.five.md",
children: [],
path: "/child/5.five",
title: "Five",
children: [],
index: 4294967295,
},
Link {
path: "/child/child_of_child/",
title: "Child of Child",
src: "child/child_of_child",
children: [
Link {
src: "child/child_of_child/6.six.md",
children: [],
path: "/child/child_of_child/6.six",
title: "Six",
children: [],
index: 4294967295,
},
Link {
src: "child/child_of_child/7.seven.md",
children: [],
path: "/child/child_of_child/7.seven",
title: "Seven",
children: [],
index: 4294967295,
},
Link {
src: "child/child_of_child/8.eight.md",
children: [],
path: "/child/child_of_child/8.eight",
title: "Eight",
children: [],
index: 4294967295,
},
Link {
src: "child/child_of_child/ASASSA.md",
children: [],
path: "/child/child_of_child/ASASSA",
title: "Child of Child",
children: [],
index: 4294967295,
},
],
index: 4294967295,
path: "/child/child_of_child/",
title: "Child of Child",
},
],
index: 4294967295,
path: "/child/",
title: "Nested Root",
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ description: With base path
---
[
Link {
src: "one.md",
children: [],
path: "/example/one",
title: "One",
children: [],
index: 4294967295,
},
Link {
src: "two.md",
children: [],
path: "/example/two",
title: "Two",
children: [],
index: 4294967295,
},
Link {
path: "/example/child/",
title: "Nested Root",
src: "child",
children: [
Link {
src: "child/three.md",
children: [],
path: "/example/child/three",
title: "Three",
children: [],
index: 4294967295,
},
],
index: 4294967295,
path: "/example/child/",
title: "Nested Root",
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Manual menu file from nested directory
---
[
Link {
src: "child/three.md",
children: [],
path: "/child/three",
title: "Three",
children: [],
index: 4294967295,
},
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ description: Manual menu file from parent directory
---
[
Link {
path: "/child/",
title: "Nested Root",
src: "child",
children: [
Link {
src: "one.md",
children: [],
path: "/one",
title: "One",
children: [],
index: 4294967295,
},
],
index: 4294967295,
path: "/child/",
title: "Nested Root",
},
]
20 changes: 10 additions & 10 deletions src/snapshots/docgen__navigation__test__manual_menu_nested.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ description: Manual menu nested
---
[
Link {
src: "one.md",
children: [],
path: "/one",
title: "One",
children: [],
index: 4294967295,
},
Link {
path: "/child/",
title: "Nested Root",
src: "child",
children: [
Link {
path: "/child/nested/",
title: "Nested Root",
src: "child/nested",
children: [
Link {
src: "child/nested/four.md",
children: [],
path: "/child/nested/four",
title: "Four",
children: [],
index: 4294967295,
},
],
index: 4294967295,
path: "/child/nested/",
title: "Nested Root",
},
],
index: 4294967295,
path: "/child/",
title: "Nested Root",
},
]
Loading

0 comments on commit b2d0cfc

Please sign in to comment.