mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-22 16:14:18 +02:00
Merge pull request #201 from tranzystorek-io/refactor-misc
Add various small refactorings
This commit is contained in:
commit
4e0d497347
6 changed files with 62 additions and 67 deletions
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
|
|
@ -15,7 +15,7 @@ jobs:
|
|||
strategy:
|
||||
matrix:
|
||||
platform: [ubuntu-latest, macos-latest, windows-latest]
|
||||
rust: [1.52, stable]
|
||||
rust: [1.53, stable]
|
||||
runs-on: ${{ matrix.platform }}
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
|
@ -60,7 +60,7 @@ jobs:
|
|||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: 1.52
|
||||
toolchain: 1.53
|
||||
override: true
|
||||
- run: rustup component add rustfmt
|
||||
- uses: actions-rs/cargo@v1
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
msrv = "1.52"
|
||||
msrv = "1.53"
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ impl PageLookupResult {
|
|||
}
|
||||
|
||||
pub fn paths(&self) -> impl Iterator<Item = &Path> {
|
||||
iter::once(self.page_path.as_path()).chain(self.patch_path.as_deref().into_iter())
|
||||
iter::once(self.page_path.as_path()).chain(self.patch_path.as_deref())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -224,7 +224,7 @@ impl Cache {
|
|||
}
|
||||
}
|
||||
|
||||
let patch_path = Self::find_patch(&patch_filename, custom_pages_dir.as_deref());
|
||||
let patch_path = Self::find_patch(&patch_filename, custom_pages_dir);
|
||||
|
||||
// Try to find a platform specific path next, append custom patch to it.
|
||||
if let Some(pf) = self.get_platform_dir() {
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ where
|
|||
.map_err(|e| WriteError(e.to_string()))?;
|
||||
}
|
||||
LineType::ExampleCode(text) => {
|
||||
writeln!(writer, " {}", &format_code(&command, &text, config))
|
||||
writeln!(writer, " {}", format_code(&command, &text, config))
|
||||
.map_err(|e| WriteError(e.to_string()))?;
|
||||
}
|
||||
LineType::Other(text) => debug!("Unknown line type: {:?}", text),
|
||||
|
|
|
|||
28
src/main.rs
28
src/main.rs
|
|
@ -17,7 +17,6 @@
|
|||
use std::fs::File;
|
||||
use std::io::BufRead;
|
||||
use std::io::BufReader;
|
||||
use std::iter;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::{env, io::Write};
|
||||
|
|
@ -315,10 +314,7 @@ fn get_languages(env_lang: Option<&str>, env_language: Option<&str>) -> Vec<Stri
|
|||
let env_lang = env_lang.unwrap();
|
||||
|
||||
// Create an iterator that contains $LANGUAGE (':' separated list) followed by $LANG (single language)
|
||||
let locales = env_language
|
||||
.unwrap_or("")
|
||||
.split(':')
|
||||
.chain(iter::once(env_lang));
|
||||
let locales = env_language.unwrap_or("").split(':').chain([env_lang]);
|
||||
|
||||
let mut lang_list = Vec::new();
|
||||
for locale in locales {
|
||||
|
|
@ -510,19 +506,19 @@ mod test {
|
|||
use docopt::{Docopt, Error};
|
||||
|
||||
fn test_helper(argv: &[&str]) -> Result<Args, Error> {
|
||||
Docopt::new(USAGE).and_then(|d| d.argv(argv.iter()).deserialize())
|
||||
Docopt::new(USAGE).and_then(|d| d.argv(argv).deserialize())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_docopt_os_case_insensitive() {
|
||||
let argv = vec!["cp", "--os", "LiNuX"];
|
||||
let argv = ["cp", "--os", "LiNuX"];
|
||||
let os = test_helper(&argv).unwrap().flag_os.unwrap();
|
||||
assert_eq!(OsType::Linux, os);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_docopt_expect_error() {
|
||||
let argv = vec!["cp", "--os", "lindows"];
|
||||
let argv = ["cp", "--os", "lindows"];
|
||||
assert!(!test_helper(&argv).is_ok());
|
||||
}
|
||||
|
||||
|
|
@ -532,41 +528,41 @@ mod test {
|
|||
#[test]
|
||||
fn missing_lang_env() {
|
||||
let lang_list = get_languages(None, Some("de:fr"));
|
||||
assert_eq!(lang_list, vec!["en"]);
|
||||
assert_eq!(lang_list, ["en"]);
|
||||
let lang_list = get_languages(None, None);
|
||||
assert_eq!(lang_list, vec!["en"]);
|
||||
assert_eq!(lang_list, ["en"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_language_env() {
|
||||
let lang_list = get_languages(Some("de"), None);
|
||||
assert_eq!(lang_list, vec!["de", "en"]);
|
||||
assert_eq!(lang_list, ["de", "en"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn preference_order() {
|
||||
let lang_list = get_languages(Some("de"), Some("fr:cn"));
|
||||
assert_eq!(lang_list, vec!["fr", "cn", "de", "en"]);
|
||||
assert_eq!(lang_list, ["fr", "cn", "de", "en"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn country_code_expansion() {
|
||||
let lang_list = get_languages(Some("pt_BR"), None);
|
||||
assert_eq!(lang_list, vec!["pt_BR", "pt", "en"]);
|
||||
assert_eq!(lang_list, ["pt_BR", "pt", "en"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore_posix_and_c() {
|
||||
let lang_list = get_languages(Some("POSIX"), None);
|
||||
assert_eq!(lang_list, vec!["en"]);
|
||||
assert_eq!(lang_list, ["en"]);
|
||||
let lang_list = get_languages(Some("C"), None);
|
||||
assert_eq!(lang_list, vec!["en"]);
|
||||
assert_eq!(lang_list, ["en"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_duplicates() {
|
||||
let lang_list = get_languages(Some("de"), Some("fr:de:cn:de"));
|
||||
assert_eq!(lang_list, vec!["fr", "de", "cn", "en"]);
|
||||
assert_eq!(lang_list, ["fr", "de", "cn", "en"]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
89
tests/lib.rs
89
tests/lib.rs
|
|
@ -37,7 +37,7 @@ impl TestEnv {
|
|||
/// 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);
|
||||
println!("Config path: {:?}", config_file_name);
|
||||
|
||||
let mut config_file = File::create(&config_file_name).unwrap();
|
||||
config_file.write_all(content.as_ref().as_bytes()).unwrap();
|
||||
|
|
@ -59,23 +59,23 @@ impl TestEnv {
|
|||
create_dir_all(&dir).unwrap();
|
||||
|
||||
let mut file = File::create(&dir.join(format!("{}.md", name))).unwrap();
|
||||
file.write_all(&contents.as_bytes()).unwrap();
|
||||
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();
|
||||
create_dir_all(dir).unwrap();
|
||||
let mut file = File::create(&dir.join(format!("{}.page", name))).unwrap();
|
||||
file.write_all(&contents.as_bytes()).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();
|
||||
create_dir_all(dir).unwrap();
|
||||
let mut file = File::create(&dir.join(format!("{}.patch", name))).unwrap();
|
||||
file.write_all(&contents.as_bytes()).unwrap();
|
||||
file.write_all(contents.as_bytes()).unwrap();
|
||||
}
|
||||
|
||||
/// Disable default features.
|
||||
|
|
@ -122,7 +122,7 @@ impl TestEnv {
|
|||
fn test_missing_cache() {
|
||||
TestEnv::new()
|
||||
.command()
|
||||
.args(&["sl"])
|
||||
.args(["sl"])
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(contains("Cache not found. Please run `tldr --update`."));
|
||||
|
|
@ -134,19 +134,19 @@ fn test_update_cache() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["sl"])
|
||||
.args(["sl"])
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(contains("Cache not found. Please run `tldr --update`."));
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--update"])
|
||||
.args(["--update"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("Successfully updated cache."));
|
||||
|
||||
testenv.command().args(&["sl"]).assert().success();
|
||||
testenv.command().args(["sl"]).assert().success();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -154,14 +154,14 @@ fn test_quiet_cache() {
|
|||
let testenv = TestEnv::new();
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--update", "--quiet"])
|
||||
.args(["--update", "--quiet"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(is_empty());
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--clear-cache", "--quiet"])
|
||||
.args(["--clear-cache", "--quiet"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(is_empty());
|
||||
|
|
@ -173,14 +173,14 @@ fn test_quiet_failures() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--update", "-q"])
|
||||
.args(["--update", "-q"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(is_empty());
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["fakeprogram", "-q"])
|
||||
.args(["fakeprogram", "-q"])
|
||||
.assert()
|
||||
.failure()
|
||||
.stdout(is_empty());
|
||||
|
|
@ -192,7 +192,7 @@ fn test_quiet_old_cache() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--update", "-q"])
|
||||
.args(["--update", "-q"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(is_empty());
|
||||
|
|
@ -205,14 +205,14 @@ fn test_quiet_old_cache() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["tldr"])
|
||||
.args(["tldr"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("The cache hasn't been updated for more than "));
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["tldr", "--quiet"])
|
||||
.args(["tldr", "--quiet"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("The cache hasn't been updated for more than ").not());
|
||||
|
|
@ -224,7 +224,7 @@ fn test_setup_seed_config() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--seed-config"])
|
||||
.args(["--seed-config"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("Successfully created seed config file here"));
|
||||
|
|
@ -236,7 +236,7 @@ fn test_show_paths() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--show-paths"])
|
||||
.args(["--show-paths"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(contains(format!(
|
||||
|
|
@ -275,7 +275,7 @@ fn test_os_specific_page() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--os", "sunos", "truss"])
|
||||
.args(["--os", "sunos", "truss"])
|
||||
.assert()
|
||||
.success();
|
||||
}
|
||||
|
|
@ -289,7 +289,7 @@ fn test_markdown_rendering() {
|
|||
let expected = include_str!("which-markdown.expected");
|
||||
testenv
|
||||
.command()
|
||||
.args(&["-m", "which"])
|
||||
.args(["-m", "which"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(similar(expected));
|
||||
|
|
@ -305,13 +305,13 @@ fn _test_correct_rendering(
|
|||
|
||||
// Create input file
|
||||
let file_path = testenv.input_dir.path().join(filename);
|
||||
println!("Testfile path: {:?}", &file_path);
|
||||
println!("Testfile path: {:?}", file_path);
|
||||
let mut file = File::create(&file_path).unwrap();
|
||||
file.write_all(input_file.as_bytes()).unwrap();
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--color", color_option, "-f", &file_path.to_str().unwrap()])
|
||||
.args(["--color", color_option, "-f", file_path.to_str().unwrap()])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(similar(expected));
|
||||
|
|
@ -370,27 +370,26 @@ fn test_correct_rendering_with_config() {
|
|||
// Setup config file
|
||||
// TODO should be config::CONFIG_FILE_NAME
|
||||
let config_file_path = testenv.config_dir.path().join("config.toml");
|
||||
println!("Config path: {:?}", &config_file_path);
|
||||
println!("Config path: {:?}", config_file_path);
|
||||
|
||||
let mut config_file = File::create(&config_file_path).unwrap();
|
||||
config_file
|
||||
.write_all(include_str!("config.toml").as_bytes())
|
||||
.write_all(include_bytes!("config.toml"))
|
||||
.unwrap();
|
||||
|
||||
// Create input file
|
||||
let file_path = testenv.input_dir.path().join("inkscape-v2.md");
|
||||
println!("Testfile path: {:?}", &file_path);
|
||||
println!("Testfile path: {:?}", file_path);
|
||||
|
||||
let mut file = File::create(&file_path).unwrap();
|
||||
file.write_all(include_str!("inkscape-v2.md").as_bytes())
|
||||
.unwrap();
|
||||
file.write_all(include_bytes!("inkscape-v2.md")).unwrap();
|
||||
|
||||
// Load expected output
|
||||
let expected = include_str!("inkscape-with-config.expected");
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--color", "always", "-f", &file_path.to_str().unwrap()])
|
||||
.args(["--color", "always", "-f", file_path.to_str().unwrap()])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(similar(expected));
|
||||
|
|
@ -402,14 +401,14 @@ fn test_spaces_find_command() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--update"])
|
||||
.args(["--update"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("Successfully updated cache."));
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["git", "checkout"])
|
||||
.args(["git", "checkout"])
|
||||
.assert()
|
||||
.success();
|
||||
}
|
||||
|
|
@ -420,14 +419,14 @@ fn test_pager_flag_enable() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--update"])
|
||||
.args(["--update"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("Successfully updated cache."));
|
||||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--pager", "which"])
|
||||
.args(["--pager", "which"])
|
||||
.assert()
|
||||
.success();
|
||||
}
|
||||
|
|
@ -438,7 +437,7 @@ fn test_list_flag_rendering() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--list"])
|
||||
.args(["--list"])
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(contains("Cache not found. Please run `tldr --update`."));
|
||||
|
|
@ -447,7 +446,7 @@ fn test_list_flag_rendering() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--list"])
|
||||
.args(["--list"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("foo\n");
|
||||
|
|
@ -458,7 +457,7 @@ fn test_list_flag_rendering() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--list"])
|
||||
.args(["--list"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout("bar\nbaz\nfoo\nqux\n");
|
||||
|
|
@ -471,7 +470,7 @@ fn test_autoupdate_cache() {
|
|||
// The first time, if automatic updates are disabled, the cache should not be found
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--list"])
|
||||
.args(["--list"])
|
||||
.assert()
|
||||
.failure()
|
||||
.stderr(contains("Cache not found. Please run `tldr --update`."));
|
||||
|
|
@ -482,14 +481,14 @@ fn test_autoupdate_cache() {
|
|||
// Activate automatic updates, set the auto-update interval to 24 hours
|
||||
let mut config_file = File::create(&config_file_path).unwrap();
|
||||
config_file
|
||||
.write_all("[updates]\nauto_update = true\nauto_update_interval_hours = 24".as_bytes())
|
||||
.write_all(b"[updates]\nauto_update = true\nauto_update_interval_hours = 24")
|
||||
.unwrap();
|
||||
config_file.flush().unwrap();
|
||||
|
||||
// Helper function that runs `tldr --list` and asserts that the cache is automatically updated
|
||||
// or not, depending on the value of `expected`.
|
||||
let check_cache_updated = |expected| {
|
||||
let assert = testenv.command().args(&["--list"]).assert().success();
|
||||
let assert = testenv.command().args(["--list"]).assert().success();
|
||||
let pred = contains("Successfully updated cache");
|
||||
if expected {
|
||||
assert.stderr(pred)
|
||||
|
|
@ -541,7 +540,7 @@ fn test_custom_page_overwrites() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["inkscape-v2", "--color", "never"])
|
||||
.args(["inkscape-v2", "--color", "never"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(similar(expected));
|
||||
|
|
@ -568,7 +567,7 @@ fn test_custom_patch_appends_to_common() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["inkscape-v2", "--color", "never"])
|
||||
.args(["inkscape-v2", "--color", "never"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(similar(expected));
|
||||
|
|
@ -598,7 +597,7 @@ fn test_custom_patch_does_not_append_to_custom() {
|
|||
|
||||
testenv
|
||||
.command()
|
||||
.args(&["inkscape-v2", "--color", "never"])
|
||||
.args(["inkscape-v2", "--color", "never"])
|
||||
.assert()
|
||||
.success()
|
||||
.stdout(similar(expected));
|
||||
|
|
@ -610,7 +609,7 @@ fn test_pager_warning() {
|
|||
let testenv = TestEnv::new();
|
||||
testenv
|
||||
.command()
|
||||
.args(&["--update"])
|
||||
.args(["--update"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("Successfully updated cache."));
|
||||
|
|
@ -618,7 +617,7 @@ fn test_pager_warning() {
|
|||
// Regular call should not show a "pager flag not available on windows" warning
|
||||
testenv
|
||||
.command()
|
||||
.args(&["which"])
|
||||
.args(["which"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("pager flag not available on Windows").not());
|
||||
|
|
@ -626,7 +625,7 @@ fn test_pager_warning() {
|
|||
// But it should be shown if the pager flag is true
|
||||
testenv
|
||||
.command()
|
||||
.args(&["which", "-p"])
|
||||
.args(["which", "-p"])
|
||||
.assert()
|
||||
.success()
|
||||
.stderr(contains("pager flag not available on Windows"));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue