Add unit tests for custom pages / patches (#183)

This commit is contained in:
Dalton 2021-04-23 13:06:44 -05:00 committed by GitHub
commit 0fce79f8ed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 182 additions and 3 deletions

View file

@ -224,20 +224,20 @@ impl Cache {
}
}
let maybe_patch = Self::find_patch(&patch_filename, custom_pages_dir.as_deref());
let patch_path = Self::find_patch(&patch_filename, custom_pages_dir.as_deref());
// Try to find a platform specific path next, append custom patch to it.
if let Some(pf) = self.get_platform_dir() {
if let Some(page) =
Self::find_page_for_platform(&page_filename, &cache_dir, pf, &lang_dirs)
{
return Some(PageLookupResult::with_page(page).with_optional_patch(maybe_patch));
return Some(PageLookupResult::with_page(page).with_optional_patch(patch_path));
}
}
// Did not find platform specific results, fall back to "common"
Self::find_page_for_platform(&page_filename, &cache_dir, "common", &lang_dirs)
.map(|page| PageLookupResult::with_page(page).with_optional_patch(maybe_patch))
.map(|page| PageLookupResult::with_page(page).with_optional_patch(patch_path))
}
/// Return the available pages.
@ -314,3 +314,27 @@ impl Cache {
Ok(())
}
}
/// Unit Tests for cache module
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_page_lookup_result_iter_with_patch() {
let lookup = PageLookupResult::with_page(PathBuf::from("test.page"))
.with_optional_patch(Some(PathBuf::from("test.patch")));
let mut iter = lookup.paths();
assert_eq!(iter.next(), Some(Path::new("test.page")));
assert_eq!(iter.next(), Some(Path::new("test.patch")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_page_lookup_result_iter_no_patch() {
let lookup = PageLookupResult::with_page(PathBuf::from("test.page"));
let mut iter = lookup.paths();
assert_eq!(iter.next(), Some(Path::new("test.page")));
assert_eq!(iter.next(), None);
}
}

View file

@ -0,0 +1,36 @@
An SVG (Scalable Vector Graphics) editing program.
Use -z to not open the GUI and only process files in the console.
Open an SVG file in the Inkscape GUI:
inkscape filename.svg
Export an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):
inkscape filename.svg -e filename.png
Export an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):
inkscape filename.svg -e filename.png -w 600 -h 400
Export a single object, given its ID, into a bitmap:
inkscape filename.svg -i id -e object.png
Export an SVG document to PDF, converting all texts to paths:
inkscape filename.svg | inkscape | inkscape --export-pdf=inkscape.pdf | inkscape | inkscape --export-text-to-path
Duplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:
inkscape filename.svg --select=path123 --verb=EditDuplicate --verb=ObjectRotate90 --verb=FileSave --verb=FileQuit
Some invalid command just to test the correct highlighting of the command name:
inkscape --use-inkscape=v3.0 file
Custom inkscape entry
My Inkscape example

5
tests/inkscape-v2.patch Normal file
View file

@ -0,0 +1,5 @@
This header shouldn't be required
=================================
Custom inkscape entry
My Inkscape example

View file

@ -12,6 +12,7 @@ use tempfile::{Builder, TempDir};
struct TestEnv {
pub cache_dir: TempDir,
pub custom_pages_dir: TempDir,
pub config_dir: TempDir,
pub input_dir: TempDir,
pub default_features: bool,
@ -23,12 +24,25 @@ impl TestEnv {
TestEnv {
cache_dir: Builder::new().prefix(".tldr.test.cache").tempdir().unwrap(),
config_dir: Builder::new().prefix(".tldr.test.conf").tempdir().unwrap(),
custom_pages_dir: Builder::new()
.prefix(".tldr.test.custom-pages")
.tempdir()
.unwrap(),
input_dir: Builder::new().prefix(".tldr.test.input").tempdir().unwrap(),
default_features: true,
features: vec![],
}
}
/// Write `content` to "config.toml" in the `config_dir` directory
fn write_config(&self, content: impl AsRef<str>) {
let config_file_name = self.config_dir.path().join("config.toml");
println!("Config path: {:?}", &config_file_name);
let mut config_file = File::create(&config_file_name).unwrap();
config_file.write(content.as_ref().as_bytes()).unwrap();
}
/// Add entry for that environment to the "common" pages.
fn add_entry(&self, name: &str, contents: &str) {
self.add_os_entry("common", name, contents);
@ -48,6 +62,22 @@ impl TestEnv {
file.write_all(&contents.as_bytes()).unwrap();
}
/// Add custom patch entry to the custom_pages_dir
fn add_page_entry(&self, name: &str, contents: &str) {
let dir = self.custom_pages_dir.path();
create_dir_all(&dir).unwrap();
let mut file = File::create(&dir.join(format!("{}.page", name))).unwrap();
file.write_all(&contents.as_bytes()).unwrap();
}
/// Add custom patch entry to the custom_pages_dir
fn add_patch_entry(&self, name: &str, contents: &str) {
let dir = self.custom_pages_dir.path();
create_dir_all(&dir).unwrap();
let mut file = File::create(&dir.join(format!("{}.patch", name))).unwrap();
file.write_all(&contents.as_bytes()).unwrap();
}
/// Disable default features.
#[allow(dead_code)] // Might be useful in the future
fn no_default_features(mut self) -> Self {
@ -495,6 +525,90 @@ fn test_autoupdate_cache() {
check_cache_updated(false);
}
/// End-end test to ensure .page files overwrite pages in cache_dir
#[test]
fn test_custom_page_overwrites() {
let testenv = TestEnv::new();
// set custom pages directory
testenv.write_config(format!(
"[directories]\ncustom_pages_dir = '{}'",
testenv.custom_pages_dir.path().to_str().unwrap()
));
// Add file that should be ignored to the cache dir
testenv.add_entry("inkscape-v2", "");
// Add .page file to custome_pages_dir
testenv.add_page_entry("inkscape-v2", include_str!("inkscape-v2.md"));
// Load expected output
let expected = include_str!("inkscape-default-no-color.expected");
testenv
.command()
.args(&["inkscape-v2", "--color", "never"])
.assert()
.success()
.stdout(similar(expected));
}
/// End-End test to ensure that .patch files are appened to pages in the cache_dir
#[test]
fn test_custom_patch_appends_to_common() {
let testenv = TestEnv::new();
// set custom pages directory
testenv.write_config(format!(
"[directories]\ncustom_pages_dir = '{}'",
testenv.custom_pages_dir.path().to_str().unwrap()
));
// Add page to the cache dir
testenv.add_entry("inkscape-v2", include_str!("inkscape-v2.md"));
// Add .page file to custome_pages_dir
testenv.add_patch_entry("inkscape-v2", include_str!("inkscape-v2.patch"));
// Load expected output
let expected = include_str!("inkscape-patched-no-color.expected");
testenv
.command()
.args(&["inkscape-v2", "--color", "never"])
.assert()
.success()
.stdout(similar(expected));
}
/// End-End test to ensure that .patch files are not appended to .page files in the custom_pages_dir
/// Maybe this interaction should change but I put this test here for the coverage
#[test]
fn test_custom_patch_does_not_append_to_custom() {
let testenv = TestEnv::new();
// set custom pages directory
testenv.write_config(format!(
"[directories]\ncustom_pages_dir = '{}'",
testenv.custom_pages_dir.path().to_str().unwrap()
));
testenv.add_entry("test", "");
// Add page to the cache dir
testenv.add_page_entry("inkscape-v2", include_str!("inkscape-v2.md"));
// Add .page file to custome_pages_dir
testenv.add_patch_entry("inkscape-v2", include_str!("inkscape-v2.patch"));
// Load expected output
let expected = include_str!("inkscape-default-no-color.expected");
testenv
.command()
.args(&["inkscape-v2", "--color", "never"])
.assert()
.success()
.stdout(similar(expected));
}
#[test]
#[cfg(target_os = "windows")]
fn test_pager_warning() {