Skip to content

Commit 49ba9dd

Browse files
src: use #[cfg(test)] mod test { } consistently
There are a few cases left over from before I learned how to do this properly. Fix them. Signed-off-by: Allison Karlitskaya <[email protected]>
1 parent 488bb7b commit 49ba9dd

File tree

2 files changed

+90
-85
lines changed

2 files changed

+90
-85
lines changed

src/bin/composefs-setup-root.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,18 +284,23 @@ fn main() -> Result<()> {
284284
setup_root(args)
285285
}
286286

287-
#[test]
288-
fn test_parse() {
289-
let failing = ["", "foo", "composefs", "composefs=foo"];
290-
for case in failing {
291-
assert!(parse_composefs_cmdline(case.as_bytes()).is_err());
287+
#[cfg(test)]
288+
mod test {
289+
use super::*;
290+
291+
#[test]
292+
fn test_parse() {
293+
let failing = ["", "foo", "composefs", "composefs=foo"];
294+
for case in failing {
295+
assert!(parse_composefs_cmdline(case.as_bytes()).is_err());
296+
}
297+
let digest = "8b7df143d91c716ecfa5fc1730022f6b421b05cedee8fd52b1fc65a96030ad52";
298+
let digest_bytes = hex::decode(digest).unwrap();
299+
similar_asserts::assert_eq!(
300+
parse_composefs_cmdline(format!("composefs={digest}").as_bytes())
301+
.unwrap()
302+
.as_slice(),
303+
&digest_bytes
304+
);
292305
}
293-
let digest = "8b7df143d91c716ecfa5fc1730022f6b421b05cedee8fd52b1fc65a96030ad52";
294-
let digest_bytes = hex::decode(digest).unwrap();
295-
similar_asserts::assert_eq!(
296-
parse_composefs_cmdline(format!("composefs={digest}").as_bytes())
297-
.unwrap()
298-
.as_slice(),
299-
&digest_bytes
300-
);
301306
}

src/oci/image.rs

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -98,82 +98,82 @@ pub fn create_image(
9898
}
9999

100100
#[cfg(test)]
101-
use crate::image::{LeafContent, RegularFile, Stat};
102-
#[cfg(test)]
103-
use std::{cell::RefCell, collections::BTreeMap, io::BufRead, path::PathBuf};
104-
105-
#[cfg(test)]
106-
fn file_entry(path: &str) -> oci::tar::TarEntry {
107-
oci::tar::TarEntry {
108-
path: PathBuf::from(path),
109-
stat: Stat {
110-
st_mode: 0o644,
111-
st_uid: 0,
112-
st_gid: 0,
113-
st_mtim_sec: 0,
114-
xattrs: RefCell::new(BTreeMap::new()),
115-
},
116-
item: oci::tar::TarItem::Leaf(LeafContent::Regular(RegularFile::Inline([].into()))),
101+
mod test {
102+
use crate::image::{LeafContent, RegularFile, Stat};
103+
use std::{cell::RefCell, collections::BTreeMap, io::BufRead, path::PathBuf};
104+
105+
use super::*;
106+
107+
fn file_entry(path: &str) -> oci::tar::TarEntry {
108+
oci::tar::TarEntry {
109+
path: PathBuf::from(path),
110+
stat: Stat {
111+
st_mode: 0o644,
112+
st_uid: 0,
113+
st_gid: 0,
114+
st_mtim_sec: 0,
115+
xattrs: RefCell::new(BTreeMap::new()),
116+
},
117+
item: oci::tar::TarItem::Leaf(LeafContent::Regular(RegularFile::Inline([].into()))),
118+
}
117119
}
118-
}
119120

120-
#[cfg(test)]
121-
fn dir_entry(path: &str) -> oci::tar::TarEntry {
122-
oci::tar::TarEntry {
123-
path: PathBuf::from(path),
124-
stat: Stat {
125-
st_mode: 0o755,
126-
st_uid: 0,
127-
st_gid: 0,
128-
st_mtim_sec: 0,
129-
xattrs: RefCell::new(BTreeMap::new()),
130-
},
131-
item: oci::tar::TarItem::Directory,
121+
fn dir_entry(path: &str) -> oci::tar::TarEntry {
122+
oci::tar::TarEntry {
123+
path: PathBuf::from(path),
124+
stat: Stat {
125+
st_mode: 0o755,
126+
st_uid: 0,
127+
st_gid: 0,
128+
st_mtim_sec: 0,
129+
xattrs: RefCell::new(BTreeMap::new()),
130+
},
131+
item: oci::tar::TarItem::Directory,
132+
}
132133
}
133-
}
134134

135-
#[cfg(test)]
136-
fn assert_files(fs: &FileSystem, expected: &[&str]) -> Result<()> {
137-
let mut out = vec![];
138-
write_dumpfile(&mut out, fs)?;
139-
let actual: Vec<String> = out
140-
.lines()
141-
.map(|line| line.unwrap().split_once(' ').unwrap().0.into())
142-
.collect();
143-
144-
similar_asserts::assert_eq!(actual, expected);
145-
Ok(())
146-
}
135+
fn assert_files(fs: &FileSystem, expected: &[&str]) -> Result<()> {
136+
let mut out = vec![];
137+
write_dumpfile(&mut out, fs)?;
138+
let actual: Vec<String> = out
139+
.lines()
140+
.map(|line| line.unwrap().split_once(' ').unwrap().0.into())
141+
.collect();
147142

148-
#[test]
149-
fn test_process_entry() -> Result<()> {
150-
let mut fs = FileSystem::new();
151-
152-
// both with and without leading slash should be supported
153-
process_entry(&mut fs, dir_entry("/a"))?;
154-
process_entry(&mut fs, dir_entry("b"))?;
155-
process_entry(&mut fs, dir_entry("c"))?;
156-
assert_files(&fs, &["/", "/a", "/b", "/c"])?;
157-
158-
// add some files
159-
process_entry(&mut fs, file_entry("/a/b"))?;
160-
process_entry(&mut fs, file_entry("/a/c"))?;
161-
process_entry(&mut fs, file_entry("/b/a"))?;
162-
process_entry(&mut fs, file_entry("/b/c"))?;
163-
process_entry(&mut fs, file_entry("/c/a"))?;
164-
process_entry(&mut fs, file_entry("/c/c"))?;
165-
assert_files(
166-
&fs,
167-
&[
168-
"/", "/a", "/a/b", "/a/c", "/b", "/b/a", "/b/c", "/c", "/c/a", "/c/c",
169-
],
170-
)?;
171-
172-
// try some whiteouts
173-
process_entry(&mut fs, file_entry(".wh.a"))?; // entire dir
174-
process_entry(&mut fs, file_entry("/b/.wh..wh.opq"))?; // opaque dir
175-
process_entry(&mut fs, file_entry("/c/.wh.c"))?; // single file
176-
assert_files(&fs, &["/", "/b", "/c", "/c/a"])?;
143+
similar_asserts::assert_eq!(actual, expected);
144+
Ok(())
145+
}
177146

178-
Ok(())
147+
#[test]
148+
fn test_process_entry() -> Result<()> {
149+
let mut fs = FileSystem::new();
150+
151+
// both with and without leading slash should be supported
152+
process_entry(&mut fs, dir_entry("/a"))?;
153+
process_entry(&mut fs, dir_entry("b"))?;
154+
process_entry(&mut fs, dir_entry("c"))?;
155+
assert_files(&fs, &["/", "/a", "/b", "/c"])?;
156+
157+
// add some files
158+
process_entry(&mut fs, file_entry("/a/b"))?;
159+
process_entry(&mut fs, file_entry("/a/c"))?;
160+
process_entry(&mut fs, file_entry("/b/a"))?;
161+
process_entry(&mut fs, file_entry("/b/c"))?;
162+
process_entry(&mut fs, file_entry("/c/a"))?;
163+
process_entry(&mut fs, file_entry("/c/c"))?;
164+
assert_files(
165+
&fs,
166+
&[
167+
"/", "/a", "/a/b", "/a/c", "/b", "/b/a", "/b/c", "/c", "/c/a", "/c/c",
168+
],
169+
)?;
170+
171+
// try some whiteouts
172+
process_entry(&mut fs, file_entry(".wh.a"))?; // entire dir
173+
process_entry(&mut fs, file_entry("/b/.wh..wh.opq"))?; // opaque dir
174+
process_entry(&mut fs, file_entry("/c/.wh.c"))?; // single file
175+
assert_files(&fs, &["/", "/b", "/c", "/c/a"])?;
176+
177+
Ok(())
178+
}
179179
}

0 commit comments

Comments
 (0)