Skip to content

Commit

Permalink
Order results by using a Vec
Browse files Browse the repository at this point in the history
  • Loading branch information
spenserblack committed Sep 26, 2023
1 parent 3f5b4c7 commit 0690413
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
source: gengo-bin/tests/test_cli.rs
expression: stdout
---
 40.79% 62 TypeScript
 40.79% 62 Plain Text
 40.79% 62 TypeScript
 18.42% 28 JavaScript

JavaScript
Expand Down
4 changes: 0 additions & 4 deletions gengo-bin/tests/test_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,26 @@ macro_rules! assert_stdout_snapshot {
}

#[test]
#[ignore = "Order is not predictable"]
#[cfg(not(feature = "color"))]
fn test_javascript_repo() {
assert_stdout_snapshot!(&["gengo", "-r", "test/javascript", "-R", ROOT]);
}

#[test]
#[cfg_attr(windows, ignore)]
#[ignore = "Order is not predictable"]
#[cfg(not(feature = "color"))]
fn test_breakdown_javascript_repo() {
assert_stdout_snapshot!(&["gengo", "-r", "test/javascript", "-R", ROOT, "--breakdown"]);
}

#[test]
#[ignore = "Order is not predictable"]
#[cfg(feature = "color")]
fn test_color_javascript_repo() {
assert_stdout_snapshot!(&["gengo", "-r", "test/javascript", "-R", ROOT]);
}

#[test]
#[cfg_attr(windows, ignore)]
#[ignore = "Order is not predictable"]
#[cfg(feature = "color")]
fn test_color_breakdown_javascript_repo() {
assert_stdout_snapshot!(&["gengo", "-r", "test/javascript", "-R", ROOT, "--breakdown"]);
Expand Down
5 changes: 2 additions & 3 deletions gengo/src/analysis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::Entry;
use indexmap::IndexMap;

use std::collections::HashMap;
use std::path::PathBuf;

pub use summary::Iter as SummaryIter;
Expand All @@ -12,10 +11,10 @@ mod summary;

/// The result of analyzing a repository along with all of its submodules.
#[derive(Debug)]
pub struct Analysis(pub(super) HashMap<PathBuf, Entry>);
pub struct Analysis(pub(super) Vec<(PathBuf, Entry)>);

impl Analysis {
pub fn iter(&self) -> impl Iterator<Item = (&PathBuf, &Entry)> + '_ {
pub fn iter(&self) -> impl Iterator<Item = &(PathBuf, Entry)> {
let results = &self.0;
results.iter()
}
Expand Down
9 changes: 2 additions & 7 deletions gengo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use documentation::Documentation;
pub use error::{Error, ErrorKind};
use generated::Generated;

use std::collections::HashMap;

pub use file_source::FileSource;
use glob::MatchOptions;
pub use languages::analyzer::Analyzers;
Expand Down Expand Up @@ -60,19 +58,16 @@ pub struct Gengo<FS: for<'fs> FileSource<'fs>> {
impl<FS: for<'fs> FileSource<'fs>> Gengo<FS> {
/// Analyzes each file in the repository at the given revision.
pub fn analyze(&self) -> Result<Analysis> {
let mut entries = HashMap::new();
// TODO Avoid this conversion to Vec?
let files: Vec<_> = self.file_source.files()?.collect();
let entries_v: Vec<_> = files
// let mut entries = Vec::with_capacity(files.len());
let entries: Vec<(_, _)> = files
.par_iter()
.filter_map(|(path, contents)| {
let entry = self.analyze_blob(path, contents)?;
Some((path.as_ref().to_owned(), entry))
})
.collect();
entries_v.into_iter().for_each(|(path, entry)| {
entries.insert(path, entry);
});

Ok(Analysis(entries))
}
Expand Down
1 change: 0 additions & 1 deletion gengo/tests/gengo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const ROOT: &str = env!("CARGO_MANIFEST_DIR");
mod util;

#[test]
#[ignore = "HashMaps have an unpredictable order"]
fn test_javascript() {
// TODO It's not great to use a snapshot test on a type that doesn't
// guarantee order. Improve this test.
Expand Down
129 changes: 72 additions & 57 deletions gengo/tests/snapshots/gengo_test__javascript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,81 @@ source: gengo/tests/gengo_test.rs
expression: results
---
Analysis(
{
"bin.js": Entry {
language: Language {
name: "JavaScript",
category: Programming,
color: "#FFFF00",
[
(
"bin.js",
Entry {
language: Language {
name: "JavaScript",
category: Programming,
color: "#FFFF00",
},
size: 28,
detectable: true,
generated: false,
documentation: false,
vendored: false,
},
size: 28,
detectable: true,
generated: false,
documentation: false,
vendored: false,
},
"src/bin.ts": Entry {
language: Language {
name: "TypeScript",
category: Programming,
color: "#0000FF",
),
(
"dist/bin.js",
Entry {
language: Language {
name: "Plain Text",
category: Prose,
color: "#000000",
},
size: 62,
detectable: true,
generated: true,
documentation: false,
vendored: false,
},
size: 62,
detectable: true,
generated: false,
documentation: false,
vendored: false,
},
"dist/bin.js": Entry {
language: Language {
name: "Plain Text",
category: Prose,
color: "#000000",
),
(
"docs/index.html",
Entry {
language: Language {
name: "HTML",
category: Markup,
color: "#FF4400",
},
size: 26,
detectable: false,
generated: false,
documentation: true,
vendored: false,
},
size: 62,
detectable: true,
generated: true,
documentation: false,
vendored: false,
},
"node_modules/my-dependency/index.js": Entry {
language: Language {
name: "JavaScript",
category: Programming,
color: "#FFFF00",
),
(
"node_modules/my-dependency/index.js",
Entry {
language: Language {
name: "JavaScript",
category: Programming,
color: "#FFFF00",
},
size: 29,
detectable: false,
generated: false,
documentation: false,
vendored: true,
},
size: 29,
detectable: false,
generated: false,
documentation: false,
vendored: true,
},
"docs/index.html": Entry {
language: Language {
name: "HTML",
category: Markup,
color: "#FF4400",
),
(
"src/bin.ts",
Entry {
language: Language {
name: "TypeScript",
category: Programming,
color: "#0000FF",
},
size: 62,
detectable: true,
generated: false,
documentation: false,
vendored: false,
},
size: 26,
detectable: false,
generated: false,
documentation: true,
vendored: false,
},
},
),
],
)

0 comments on commit 0690413

Please sign in to comment.