Skip to content

Commit 0756f2d

Browse files
committed
feat: add tests (unfinished)
1 parent 4bb89ef commit 0756f2d

File tree

5 files changed

+544
-223
lines changed

5 files changed

+544
-223
lines changed

build.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ pub fn main() {
55

66
#[cfg(feature = "binary")]
77
mod binary {
8-
use std::{
9-
collections::HashMap,
10-
fs,
11-
path::{Path, PathBuf},
12-
};
8+
use std::{collections::HashMap, fs, path::Path};
139

1410
use system_deps_meta::{read_metadata, Binary, BinaryPaths, BUILD_TARGET_DIR};
1511

@@ -21,8 +17,7 @@ mod binary {
2117
println!("cargo:rustc-env=BINARY_CONFIG={}", dest_path.display());
2218

2319
// Read metadata from the crate graph
24-
let manifest = PathBuf::from(system_deps_meta::BUILD_MANIFEST);
25-
let metadata = read_metadata(&manifest, "system-deps");
20+
let metadata = read_metadata(system_deps_meta::BUILD_MANIFEST, "system-deps");
2621

2722
// Download the binaries and get their pkg_config paths
2823
let mut binaries = metadata

meta/src/binary.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ pub struct Binary {
5252
/// The `system-deps` formatted name of another library which has binaries specified.
5353
/// This library will alias the configuration of the followed one. If `url` is specified
5454
/// alongside this field, it will no longer follow the original configuration.
55-
follows: Option<String>,
55+
_follows: Option<String>,
5656
}
5757

5858
impl Binary {
5959
pub fn paths(&self, name: &str) -> Result<HashSet<PathBuf>, Error> {
60-
// Set this binary to follow
60+
// TODO: Set this binary to follow
6161
//if let Some(follows) = self.follows {
6262
// follow_list.insert(name.clone(), follows);
6363
//}
@@ -176,7 +176,7 @@ pub fn get_path(name: &str) -> &[&'static str] {{
176176
/// Checks if the target directory is valid and if binaries need to be redownloaded.
177177
/// On an `Ok` result, if the value is true it means that the directory is correct.
178178
fn check_valid_dir(dst: &Path, checksum: Option<&str>) -> Result<bool, Error> {
179-
let e = |e| Error::InvalidDirectory(e);
179+
let e = Error::InvalidDirectory;
180180

181181
// If it doesn't exist yet the download will need to happen
182182
if !dst.try_exists().map_err(e)? {
@@ -224,7 +224,7 @@ fn download(url: &str, dst: &Path) -> Result<(), Error> {
224224
let path = Path::new(file_path);
225225
match ext {
226226
Ok(ext) => {
227-
let file = fs::read(path).map_err(|e| Error::LocalFileError(e))?;
227+
let file = fs::read(path).map_err(Error::LocalFileError)?;
228228
decompress(&file, dst, ext)?;
229229
}
230230
Err(e) => {
@@ -234,11 +234,10 @@ fn download(url: &str, dst: &Path) -> Result<(), Error> {
234234
}
235235
if !dst.read_link().is_ok_and(|l| l == path) {
236236
#[cfg(unix)]
237-
std::os::unix::fs::symlink(file_path, dst)
238-
.map_err(|e| Error::SymlinkError(e))?;
237+
std::os::unix::fs::symlink(file_path, dst).map_err(Error::SymlinkError)?;
239238
#[cfg(windows)]
240239
std::os::windows::fs::symlink_dir(file_path, dst)
241-
.map_err(|e| Error::SymlinkError(e))?;
240+
.map_err(Error::SymlinkError)?;
242241
}
243242
}
244243
};
@@ -250,7 +249,9 @@ fn download(url: &str, dst: &Path) -> Result<(), Error> {
250249
#[cfg(feature = "web")]
251250
{
252251
let ext = ext?;
253-
let file = reqwest::blocking::get(url).and_then(|req| req.bytes())?;
252+
let file = reqwest::blocking::get(url)
253+
.and_then(|req| req.bytes())
254+
.map_err(Error::DownloadError)?;
254255
decompress(&file, dst, ext)?;
255256
}
256257
}
@@ -265,7 +266,7 @@ fn download(url: &str, dst: &Path) -> Result<(), Error> {
265266
fn decompress(file: &[u8], dst: &Path, ext: Extension) -> Result<(), Error> {
266267
#[cfg(any(feature = "gz", feature = "xz", feature = "zip", feature = "pkg"))]
267268
{
268-
let e = |e| Error::DecompressError(e);
269+
let e = Error::DecompressError;
269270

270271
match ext {
271272
#[cfg(feature = "gz")]

meta/src/lib.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,33 @@ pub const BUILD_TARGET_DIR: &str = env!("BUILD_TARGET_DIR");
2020
/// Metadata related errors.
2121
#[derive(Debug)]
2222
pub enum Error {
23+
// Meta
24+
PackageNotFound(String),
25+
IncompatibleMerge,
26+
SerializationError(serde_json::Error),
2327
// Binary
2428
DecompressError(io::Error),
29+
DownloadError(reqwest::Error),
2530
DirectoryIsFile(String),
2631
InvalidDirectory(io::Error),
2732
InvalidExtension(String),
2833
LocalFileError(io::Error),
2934
SymlinkError(io::Error),
30-
// Web
31-
DownloadError(reqwest::Error),
32-
}
33-
34-
impl From<reqwest::Error> for Error {
35-
fn from(e: reqwest::Error) -> Self {
36-
Self::DownloadError(e)
37-
}
3835
}
3936

4037
impl fmt::Display for Error {
4138
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4239
match self {
40+
Error::PackageNotFound(pkg) => write!(f, "Package not found {}", pkg),
41+
Error::IncompatibleMerge => write!(f, "Can't merge metadata"),
42+
Error::SerializationError(e) => write!(f, "Error while parsing: {:?}", e),
4343
Error::DecompressError(e) => {
4444
write!(f, "Error while decompressing the binaries: {:?}", e)
4545
}
46-
Error::DirectoryIsFile(dir) => write!(f, "The target directory is a file {:?}", dir),
46+
Error::DirectoryIsFile(dir) => write!(f, "The target directory is a file {}", dir),
4747
Error::InvalidDirectory(e) => write!(f, "The binary directory is not valid: {:?}", e),
4848
Error::InvalidExtension(url) => {
49-
write!(f, "Unsuppported binary extension for {:?}", url)
49+
write!(f, "Unsuppported binary extension for {}", url)
5050
}
5151
Error::LocalFileError(e) => {
5252
write!(f, "Error reading the local binary file: {:?}", e)

0 commit comments

Comments
 (0)