• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tamada / totebag / 10414561419

16 Aug 2024 04:16AM UTC coverage: 81.322% (-0.1%) from 81.417%
10414561419

push

github

web-flow
Merge pull request #34 from tamada/release/v0.6.0

Release/v0.6.0

275 of 322 new or added lines in 12 files covered. (85.4%)

2 existing lines in 1 file now uncovered.

1206 of 1483 relevant lines covered (81.32%)

6.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

74.31
/src/extractor.rs
1
use std::path::PathBuf;
2

3
use crate::cli::{Result, ToteError};
4
use crate::format::{find_format, Format};
5
use crate::verboser::{create_verboser, Verboser};
6
use crate::CliOpts;
7

8
mod cab;
9
mod lha;
10
mod rar;
11
mod sevenz;
12
mod tar;
13
mod zip;
14

15
pub struct ExtractorOpts {
16
    pub dest: PathBuf,
17
    pub use_archive_name_dir: bool,
18
    pub overwrite: bool,
19
    pub v: Box<dyn Verboser>,
20
}
21

22
impl ExtractorOpts {
23
    pub fn new(opts: &CliOpts) -> ExtractorOpts {
×
24
        let d = opts.output.clone();
×
25
        ExtractorOpts {
×
26
            dest: d.unwrap_or_else(|| PathBuf::from(".")),
×
27
            use_archive_name_dir: opts.to_archive_name_dir,
×
28
            overwrite: opts.overwrite,
×
29
            v: create_verboser(opts.verbose),
×
30
        }
×
31
    }
×
32

33
    /// Returns the base of the destination directory for the archive file.
34
    /// The target is the archive file name of source.
35
    pub fn destination(&self, target: &PathBuf) -> Result<PathBuf> {
101✔
36
        let dest = self.destination_file(target);
101✔
37
        if dest.exists() && !self.overwrite {
101✔
38
            Err(ToteError::FileExists(dest.clone()))
1✔
39
        } else {
40
            Ok(dest)
100✔
41
        }
42
    }
101✔
43

44
    fn destination_file(&self, target: &PathBuf) -> PathBuf {
101✔
45
        if self.use_archive_name_dir {
101✔
46
            let file_name = target.file_name().unwrap().to_str().unwrap();
62✔
47
            let ext = target.extension().unwrap().to_str().unwrap();
62✔
48
            let dir_name = file_name
62✔
49
                .trim_end_matches(ext)
62✔
50
                .trim_end_matches(".")
62✔
51
                .to_string();
62✔
52
            self.dest.join(dir_name)
62✔
53
        } else {
54
            self.dest.clone()
39✔
55
        }
56
    }
101✔
57
}
58

59
pub trait Extractor {
60
    fn list_archives(&self, archive_file: PathBuf) -> Result<Vec<String>>;
61
    fn perform(&self, archive_file: PathBuf, opts: &ExtractorOpts) -> Result<()>;
62
    fn format(&self) -> Format;
63
}
64

65
pub fn create_extractor(file: &PathBuf) -> Result<Box<dyn Extractor>> {
8✔
66
    let format = find_format(file.file_name());
8✔
67
    match format {
8✔
68
        Ok(format) => {
8✔
69
            return match format {
8✔
NEW
70
                Format::Cab => Ok(Box::new(cab::CabExtractor {})),
×
NEW
71
                Format::LHA => Ok(Box::new(lha::LhaExtractor {})),
×
72
                Format::Rar => Ok(Box::new(rar::RarExtractor {})),
1✔
73
                Format::SevenZ => Ok(Box::new(sevenz::SevenZExtractor {})),
1✔
74
                Format::Tar => Ok(Box::new(tar::TarExtractor {})),
1✔
75
                Format::TarBz2 => Ok(Box::new(tar::TarBz2Extractor {})),
1✔
76
                Format::TarGz => Ok(Box::new(tar::TarGzExtractor {})),
1✔
77
                Format::TarXz => Ok(Box::new(tar::TarXzExtractor {})),
1✔
78
                Format::TarZstd => Ok(Box::new(tar::TarZstdExtractor {})),
×
79
                Format::Zip => Ok(Box::new(zip::ZipExtractor {})),
1✔
80
                Format::Unknown(s) => Err(ToteError::UnknownFormat(format!(
1✔
81
                    "{}: unsupported format",
1✔
82
                    s
1✔
83
                ))),
1✔
84
            }
85
        }
86
        Err(msg) => Err(msg),
×
87
    }
88
}
8✔
89

90
pub fn extractor_info(
×
91
    extractor: &Box<dyn Extractor>,
×
92
    target: &PathBuf,
×
93
    opts: &ExtractorOpts,
×
94
) -> String {
×
95
    format!(
×
96
        "Format: {:?}\nFile: {:?}\nDestination: {:?}",
×
97
        extractor.format(),
×
98
        target,
×
99
        opts.dest,
×
100
    )
×
101
}
×
102

103
#[cfg(test)]
104
mod tests {
105
    use super::*;
106

107
    #[test]
108
    fn test_destination() {
1✔
109
        let opts1 = ExtractorOpts {
1✔
110
            dest: PathBuf::from("."),
1✔
111
            use_archive_name_dir: true,
1✔
112
            overwrite: false,
1✔
113
            v: create_verboser(false),
1✔
114
        };
1✔
115
        let target = PathBuf::from("/tmp/archive.zip");
1✔
116

117
        if let Ok(t) = opts1.destination(&target) {
1✔
118
            assert_eq!(t, PathBuf::from("./archive"));
1✔
119
        }
×
120

121
        let opts2 = ExtractorOpts {
1✔
122
            dest: PathBuf::from("."),
1✔
123
            use_archive_name_dir: false,
1✔
124
            overwrite: false,
1✔
125
            v: create_verboser(false),
1✔
126
        };
1✔
127
        let target = PathBuf::from("/tmp/archive.zip");
1✔
128
        if let Ok(t) = opts2.destination(&target) {
1✔
129
            assert_eq!(t, PathBuf::from("."));
×
130
        }
1✔
131
    }
1✔
132

133
    #[test]
134
    fn test_create_extractor() {
1✔
135
        let e1 = create_extractor(&PathBuf::from("results/test.zip"));
1✔
136
        assert!(e1.is_ok());
1✔
137
        assert_eq!(e1.unwrap().format(), Format::Zip);
1✔
138

139
        let e2 = create_extractor(&PathBuf::from("results/test.tar"));
1✔
140
        assert!(e2.is_ok());
1✔
141
        assert_eq!(e2.unwrap().format(), Format::Tar);
1✔
142

143
        let e3 = create_extractor(&PathBuf::from("results/test.tgz"));
1✔
144
        assert!(e3.is_ok());
1✔
145
        assert_eq!(e3.unwrap().format(), Format::TarGz);
1✔
146

147
        let e4 = create_extractor(&PathBuf::from("results/test.tbz2"));
1✔
148
        assert!(e4.is_ok());
1✔
149
        assert_eq!(e4.unwrap().format(), Format::TarBz2);
1✔
150

151
        let e5 = create_extractor(&PathBuf::from("results/test.rar"));
1✔
152
        assert!(e5.is_ok());
1✔
153
        assert_eq!(e5.unwrap().format(), Format::Rar);
1✔
154

155
        let e6 = create_extractor(&PathBuf::from("results/test.tar.xz"));
1✔
156
        assert!(e6.is_ok());
1✔
157
        assert_eq!(e6.unwrap().format(), Format::TarXz);
1✔
158

159
        let e7 = create_extractor(&PathBuf::from("results/test.7z"));
1✔
160
        assert!(e7.is_ok());
1✔
161
        assert_eq!(e7.unwrap().format(), Format::SevenZ);
1✔
162

163
        let e8 = create_extractor(&PathBuf::from("results/test.unknown"));
1✔
164
        assert!(e8.is_err());
1✔
165
        if let Err(ToteError::UnknownFormat(msg)) = e8 {
1✔
166
            assert_eq!(msg, "test.unknown: unsupported format".to_string());
1✔
167
        } else {
168
            assert!(false);
×
169
        }
170
    }
1✔
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc