Skip to content

Commit 1a5bcfe

Browse files
authored
Share entry struct with integration tests (#87)
1 parent 2f57fa5 commit 1a5bcfe

File tree

11 files changed

+120
-120
lines changed

11 files changed

+120
-120
lines changed

src/entry.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::*;
22

33
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
44
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
5-
pub(crate) struct Entry {
6-
pub(crate) hash: Hash,
7-
pub(crate) size: u64,
5+
pub struct Entry {
6+
pub hash: Hash,
7+
pub size: u64,
88
}

src/hash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22

33
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
4-
pub(crate) struct Hash(blake3::Hash);
4+
pub struct Hash(blake3::Hash);
55

66
impl Hash {
77
pub(crate) const LEN: usize = blake3::OUT_LEN;

src/lib.rs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
use {
2+
self::{
3+
arguments::Arguments, display_path::DisplayPath, display_secret::DisplaySecret, error::Error,
4+
lint::Lint, lint_group::LintGroup, list::List, manifest::Manifest, metadata::Metadata,
5+
options::Options, owo_colorize_ext::OwoColorizeExt, private_key::PrivateKey,
6+
public_key::PublicKey, relative_path::RelativePath, signature::Signature,
7+
signature_error::SignatureError, style::Style, subcommand::Subcommand, template::Template,
8+
utf8_path_ext::Utf8PathExt,
9+
},
10+
blake3::Hasher,
11+
camino::{Utf8Component, Utf8Path, Utf8PathBuf},
12+
clap::{Parser, ValueEnum},
13+
indicatif::{ProgressBar, ProgressStyle},
14+
lexiclean::Lexiclean,
15+
owo_colors::Styled,
16+
serde::{Deserialize, Deserializer, Serialize, Serializer},
17+
serde_with::{DeserializeFromStr, SerializeDisplay},
18+
snafu::{ensure, ErrorCompat, OptionExt, ResultExt, Snafu},
19+
std::{
20+
array::TryFromSliceError,
21+
backtrace::{Backtrace, BacktraceStatus},
22+
cmp::Ordering,
23+
collections::{BTreeMap, HashMap},
24+
env,
25+
fmt::{self, Display, Formatter},
26+
fs::File,
27+
io::{self, IsTerminal},
28+
path::{Path, PathBuf},
29+
process,
30+
str::{self, FromStr},
31+
},
32+
walkdir::WalkDir,
33+
};
34+
35+
pub use self::{entry::Entry, hash::Hash};
36+
37+
#[cfg(test)]
38+
use assert_fs::TempDir;
39+
40+
mod arguments;
41+
mod display_path;
42+
mod display_secret;
43+
mod entry;
44+
mod error;
45+
mod filesystem;
46+
mod hash;
47+
mod lint;
48+
mod lint_group;
49+
mod list;
50+
mod manifest;
51+
mod metadata;
52+
mod options;
53+
mod owo_colorize_ext;
54+
mod private_key;
55+
mod progress_bar;
56+
mod public_key;
57+
mod relative_path;
58+
mod signature;
59+
mod signature_error;
60+
mod style;
61+
mod subcommand;
62+
mod template;
63+
mod utf8_path_ext;
64+
65+
type Result<T = (), E = Error> = std::result::Result<T, E>;
66+
67+
const MASTER_PRIVATE_KEY: &str = "master.private";
68+
const MASTER_PUBLIC_KEY: &str = "master.public";
69+
70+
fn current_dir() -> Result<Utf8PathBuf> {
71+
Utf8PathBuf::from_path_buf(env::current_dir().context(error::CurrentDir)?)
72+
.map_err(|path| error::PathUnicode { path }.build())
73+
}
74+
75+
fn decode_path(path: &Path) -> Result<&Utf8Path> {
76+
Utf8Path::from_path(path).context(error::PathUnicode { path })
77+
}
78+
79+
pub fn run() {
80+
if let Err(err) = Arguments::parse().run() {
81+
let style = Style::stderr();
82+
eprintln!(
83+
"{}: {}",
84+
"error".style(style.error()),
85+
err.style(style.message()),
86+
);
87+
88+
let causes = err.iter_chain().skip(1).count();
89+
90+
for (i, err) in err.iter_chain().skip(1).enumerate() {
91+
eprintln!(" {}─ {err}", if i < causes - 1 { '├' } else { '└' });
92+
}
93+
94+
if let Some(backtrace) = err.backtrace() {
95+
if backtrace.status() == BacktraceStatus::Captured {
96+
eprintln!();
97+
eprintln!("backtrace:");
98+
eprintln!("{backtrace}");
99+
}
100+
}
101+
102+
process::exit(1);
103+
}
104+
}

src/main.rs

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,3 @@
1-
use {
2-
self::{
3-
arguments::Arguments, display_path::DisplayPath, display_secret::DisplaySecret, entry::Entry,
4-
error::Error, hash::Hash, lint::Lint, lint_group::LintGroup, list::List, manifest::Manifest,
5-
metadata::Metadata, options::Options, owo_colorize_ext::OwoColorizeExt,
6-
private_key::PrivateKey, public_key::PublicKey, relative_path::RelativePath,
7-
signature::Signature, signature_error::SignatureError, style::Style, subcommand::Subcommand,
8-
template::Template, utf8_path_ext::Utf8PathExt,
9-
},
10-
blake3::Hasher,
11-
camino::{Utf8Component, Utf8Path, Utf8PathBuf},
12-
clap::{Parser, ValueEnum},
13-
indicatif::{ProgressBar, ProgressStyle},
14-
lexiclean::Lexiclean,
15-
owo_colors::Styled,
16-
serde::{Deserialize, Deserializer, Serialize, Serializer},
17-
serde_with::{DeserializeFromStr, SerializeDisplay},
18-
snafu::{ensure, ErrorCompat, OptionExt, ResultExt, Snafu},
19-
std::{
20-
array::TryFromSliceError,
21-
backtrace::{Backtrace, BacktraceStatus},
22-
cmp::Ordering,
23-
collections::{BTreeMap, HashMap},
24-
env,
25-
fmt::{self, Display, Formatter},
26-
fs::File,
27-
io::{self, IsTerminal},
28-
path::{Path, PathBuf},
29-
process,
30-
str::{self, FromStr},
31-
},
32-
walkdir::WalkDir,
33-
};
34-
35-
#[cfg(test)]
36-
use assert_fs::TempDir;
37-
38-
mod arguments;
39-
mod display_path;
40-
mod display_secret;
41-
mod entry;
42-
mod error;
43-
mod filesystem;
44-
mod hash;
45-
mod lint;
46-
mod lint_group;
47-
mod list;
48-
mod manifest;
49-
mod metadata;
50-
mod options;
51-
mod owo_colorize_ext;
52-
mod private_key;
53-
mod progress_bar;
54-
mod public_key;
55-
mod relative_path;
56-
mod signature;
57-
mod signature_error;
58-
mod style;
59-
mod subcommand;
60-
mod template;
61-
mod utf8_path_ext;
62-
63-
type Result<T = (), E = Error> = std::result::Result<T, E>;
64-
65-
const MASTER_PRIVATE_KEY: &str = "master.private";
66-
const MASTER_PUBLIC_KEY: &str = "master.public";
67-
68-
fn current_dir() -> Result<Utf8PathBuf> {
69-
Utf8PathBuf::from_path_buf(env::current_dir().context(error::CurrentDir)?)
70-
.map_err(|path| error::PathUnicode { path }.build())
71-
}
72-
73-
fn decode_path(path: &Path) -> Result<&Utf8Path> {
74-
Utf8Path::from_path(path).context(error::PathUnicode { path })
75-
}
76-
771
fn main() {
78-
if let Err(err) = Arguments::parse().run() {
79-
let style = Style::stderr();
80-
eprintln!(
81-
"{}: {}",
82-
"error".style(style.error()),
83-
err.style(style.message()),
84-
);
85-
86-
let causes = err.iter_chain().skip(1).count();
87-
88-
for (i, err) in err.iter_chain().skip(1).enumerate() {
89-
eprintln!(" {}─ {err}", if i < causes - 1 { '├' } else { '└' });
90-
}
91-
92-
if let Some(backtrace) = err.backtrace() {
93-
if backtrace.status() == BacktraceStatus::Captured {
94-
eprintln!();
95-
eprintln!("backtrace:");
96-
eprintln!("{backtrace}");
97-
}
98-
}
99-
100-
process::exit(1);
101-
}
2+
filepack::run();
1023
}

src/manifest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl Manifest {
4545
Ok((path, manifest))
4646
}
4747

48-
pub(crate) fn store(&self, path: &Utf8Path) -> Result<()> {
48+
pub(crate) fn save(&self, path: &Utf8Path) -> Result<()> {
4949
filesystem::write(path, format!("{}\n", serde_json::to_string(self).unwrap()))
5050
}
5151
}

src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) struct Metadata {
99
impl Metadata {
1010
pub(crate) const FILENAME: &'static str = "metadata.json";
1111

12-
pub(crate) fn store(&self, path: &Utf8Path) -> Result<()> {
12+
pub(crate) fn save(&self, path: &Utf8Path) -> Result<()> {
1313
filesystem::write(path, format!("{}\n", serde_json::to_string(self).unwrap()))
1414
}
1515
}

src/subcommand/create.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Create {
4040
self.force || !filesystem::exists(&path)?,
4141
error::MetadataAlreadyExists { path: &path },
4242
}
43-
Metadata::from(template).store(&path)?;
43+
Metadata::from(template).save(&path)?;
4444
}
4545

4646
let cleaned_manifest = current_dir.join(&manifest_path).lexiclean();
@@ -185,7 +185,7 @@ impl Create {
185185
manifest.signatures.insert(public_key, signature);
186186
}
187187

188-
manifest.store(&manifest_path)?;
188+
manifest.save(&manifest_path)?;
189189

190190
Ok(())
191191
}

src/subcommand/sign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl Sign {
3737

3838
manifest.signatures.insert(public_key, signature);
3939

40-
manifest.store(&path)?;
40+
manifest.save(&path)?;
4141

4242
Ok(())
4343
}

tests/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use {
55
fixture::{FileTouch, FileWriteBin, FileWriteStr, PathChild, PathCreateDir},
66
TempDir,
77
},
8+
filepack::Entry,
89
predicates::str::RegexPredicate,
910
serde::{Deserialize, Serialize},
1011
std::{collections::BTreeMap, fs, path::Path, str},
@@ -33,18 +34,12 @@ struct Manifest {
3334
signatures: BTreeMap<String, String>,
3435
}
3536

36-
#[derive(Deserialize, Serialize)]
37-
pub(crate) struct Entry {
38-
pub(crate) hash: String,
39-
pub(crate) size: u64,
40-
}
41-
4237
impl Manifest {
4338
fn load(path: &Path) -> Self {
4439
serde_json::from_str(&fs::read_to_string(path).unwrap()).unwrap()
4540
}
4641

47-
fn store(&self, path: &Path) {
42+
fn save(&self, path: &Path) {
4843
fs::write(path, serde_json::to_string(self).unwrap()).unwrap();
4944
}
5045
}

tests/sign.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn existing_signatures_must_be_valid() {
7979
"0".repeat(128),
8080
);
8181

82-
manifest.store(&dir.child("foo/filepack.json"));
82+
manifest.save(&dir.child("foo/filepack.json"));
8383

8484
Command::cargo_bin("filepack")
8585
.unwrap()

0 commit comments

Comments
 (0)