Skip to content

Commit fe9aaa0

Browse files
authored
Merge pull request #441 from rekka/feat-433-test-local-cache-tar-bundle
Test local cache and tar bundle
2 parents 6ea22a0 + 7b19dc7 commit fe9aaa0

File tree

6 files changed

+558
-15
lines changed

6 files changed

+558
-15
lines changed

Cargo.lock

+74
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@ serialization = ["serde"]
7070
# libz-sys = "^1.0"
7171

7272
[dev-dependencies]
73+
futures = "0.1"
74+
headers = "0.2"
75+
hyper = "0.12"
7376
tempfile = "^3.1"
77+
tokio = "0.1.22"

src/app_dirs.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright 2019 the Tectonic Project
22
// Licensed under the MIT License.
33

4-
use app_dirs::{AppDataType, AppDirsError};
4+
use crate::errors::Result;
5+
use app_dirs::AppDataType;
56
use std::path::PathBuf;
67

78
pub use app_dirs::sanitized;
@@ -11,14 +12,14 @@ const APP_INFO: app_dirs::AppInfo = app_dirs::AppInfo {
1112
author: "TectonicProject",
1213
};
1314

14-
pub fn user_config() -> Result<PathBuf, AppDirsError> {
15-
app_dirs::app_root(AppDataType::UserConfig, &APP_INFO)
15+
pub fn user_config() -> Result<PathBuf> {
16+
Ok(app_dirs::app_root(AppDataType::UserConfig, &APP_INFO)?)
1617
}
1718

18-
pub fn get_user_config() -> Result<PathBuf, AppDirsError> {
19-
app_dirs::get_app_root(AppDataType::UserConfig, &APP_INFO)
19+
pub fn get_user_config() -> Result<PathBuf> {
20+
Ok(app_dirs::get_app_root(AppDataType::UserConfig, &APP_INFO)?)
2021
}
2122

22-
pub fn user_cache_dir(path: &str) -> Result<PathBuf, AppDirsError> {
23-
app_dirs::app_dir(AppDataType::UserCache, &APP_INFO, path)
23+
pub fn user_cache_dir(path: &str) -> Result<PathBuf> {
24+
Ok(app_dirs::app_dir(AppDataType::UserCache, &APP_INFO, path)?)
2425
}

src/bin/tectonic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ fn inner(
125125
sess_builder.bundle(Box::new(config.make_cached_url_provider(
126126
&u,
127127
only_cached,
128+
None,
128129
status,
129130
)?));
130131
} else {

src/config.rs

+22-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010
//! running the command-line client. So we begrudgingly have a *little*
1111
//! configuration.
1212
13+
use error_chain::bail;
1314
#[cfg(feature = "serde")]
1415
use serde::{Deserialize, Serialize};
1516
use std::ffi::OsStr;
16-
use std::fs::File;
17-
use std::path::PathBuf;
17+
use std::fs::{self, File};
18+
use std::path::{Path, PathBuf};
1819
use std::sync::atomic::{AtomicBool, Ordering};
1920

2021
use crate::app_dirs;
22+
use crate::ctry;
2123
use crate::errors::{ErrorKind, Result};
2224
use crate::io::itarbundle::{HttpITarIoFactory, ITarBundle};
2325
use crate::io::local_cache::LocalCache;
@@ -110,18 +112,19 @@ impl PersistentConfig {
110112
&self,
111113
url: &str,
112114
only_cached: bool,
115+
custom_cache_root: Option<&Path>,
113116
status: &mut dyn StatusBackend,
114117
) -> Result<Box<dyn Bundle>> {
115118
let itb = ITarBundle::<HttpITarIoFactory>::new(url);
116119

117-
let mut url2digest_path = app_dirs::user_cache_dir("urls")?;
120+
let mut url2digest_path = cache_dir("urls", custom_cache_root)?;
118121
url2digest_path.push(app_dirs::sanitized(url));
119122

120123
let bundle = LocalCache::<ITarBundle<HttpITarIoFactory>>::new(
121124
itb,
122125
&url2digest_path,
123-
&app_dirs::user_cache_dir("manifests")?,
124-
&app_dirs::user_cache_dir("files")?,
126+
&cache_dir("manifests", custom_cache_root)?,
127+
&cache_dir("files", custom_cache_root)?,
125128
only_cached,
126129
status,
127130
)?;
@@ -134,8 +137,6 @@ impl PersistentConfig {
134137
file_path: &OsStr,
135138
_status: &mut dyn StatusBackend,
136139
) -> Result<Box<dyn Bundle>> {
137-
use std::path::Path;
138-
139140
let zip_bundle = ZipBundle::<File>::open(Path::new(file_path))?;
140141

141142
Ok(Box::new(zip_bundle) as _)
@@ -172,7 +173,7 @@ impl PersistentConfig {
172173
return Ok(Box::new(zip_bundle) as _);
173174
}
174175
let bundle =
175-
self.make_cached_url_provider(&self.default_bundles[0].url, only_cached, status)?;
176+
self.make_cached_url_provider(&self.default_bundles[0].url, only_cached, None, status)?;
176177
Ok(Box::new(bundle) as _)
177178
}
178179

@@ -194,3 +195,16 @@ impl Default for PersistentConfig {
194195
}
195196
}
196197
}
198+
199+
fn cache_dir(path: &str, custom_cache_root: Option<&Path>) -> Result<PathBuf> {
200+
if let Some(root) = custom_cache_root {
201+
if !root.is_dir() {
202+
bail!("Custom cache path {} is not a directory", root.display());
203+
}
204+
let full_path = root.join(path);
205+
ctry!(fs::create_dir_all(&full_path); "failed to create directory {}", full_path.display());
206+
Ok(full_path)
207+
} else {
208+
app_dirs::user_cache_dir(path)
209+
}
210+
}

0 commit comments

Comments
 (0)