From e4a281f056ed2ba071a3d729e00cd3fc2c05a6c0 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 16:12:38 +0200 Subject: [PATCH 01/25] adding cargo fmted files --- build.rs | 60 ++++++++++++++++++++++++++++++++++++++++++ src/config/template.rs | 6 +++-- 2 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 build.rs diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..1a0f83c --- /dev/null +++ b/build.rs @@ -0,0 +1,60 @@ +use std::fs; +use std::io::Result; + +const TEMPLATE_TEMPLATE: &str = r##" +# # tool-sync default configuration file +# https://github.com/chshersh/tool-sync +# This file was automatically generated by tool-sync +##################################################### +# +# +# store_directory = "$HOME/.local/bin" +# +# tool-sync provides native support for some of the tools without the need to configure them +# Uncomment the tools you want to have them +# +# [bat] +# [difftastic] +# [fd] +# [ripgrep] +# +# To add configuration for other tools these are the config options: +# [ripgrep] +# owner = "BurntSushi" +# repo = "ripgrep" +# exe_name = "rg" +# +# # Uncomment to download a specific version or tag. +# # Without this tag latest will be used +# # tag = "13.0.0" +# +# +# Asset name to download on linux OSes +# asset_name.linux = "x86_64-unknown-linux-musl" +# +# uncomment if you want to install on macOS as well +# asset_name.macos = "apple-darwin" +# +# uncomment if you want to install on Windows as well +# asset_name.windows = "x86_64-pc-windows-msvc" +"##; + +fn main() -> Result<()> { + let s = format_template(); + fs::write("src/config/template.rs", s)?; + + Ok(()) +} + +fn format_template() -> String { + let current_version = std::env::var("CARGO_PKG_VERSION").unwrap(); + + format!( + r####"//This file is generated by the build.rs script. If you want to edit this file change the template there. + +pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version {} +{}"##; +"####, + current_version, TEMPLATE_TEMPLATE + ) +} diff --git a/src/config/template.rs b/src/config/template.rs index c966172..97da895 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -1,6 +1,8 @@ -/// This file only holds the template that is used to generate a default .tools.toml. +//This file is generated by the build.rs script. If you want to edit this file change the template there. -pub const CONFIG_TEMPLATE: &str = r##"# # tool-sync default configuration file +pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version 0.1.0 + +# # tool-sync default configuration file # https://github.com/chshersh/tool-sync # This file was automatically generated by tool-sync ##################################################### From 56910b347dbb2f5053d24c816e2c43dca60659bb Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 16:18:04 +0200 Subject: [PATCH 02/25] Added comment back that was deleted accidentally --- build.rs | 4 +++- src/config/template.rs | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 1a0f83c..e7a5aed 100644 --- a/build.rs +++ b/build.rs @@ -50,7 +50,9 @@ fn format_template() -> String { let current_version = std::env::var("CARGO_PKG_VERSION").unwrap(); format!( - r####"//This file is generated by the build.rs script. If you want to edit this file change the template there. + r####"// This file only holds the template that is used to generate a default .tools.toml. +//This file is generated by the build.rs script. If you want to edit this file change the template +//there. pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version {} {}"##; diff --git a/src/config/template.rs b/src/config/template.rs index 97da895..c552a17 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -1,4 +1,6 @@ -//This file is generated by the build.rs script. If you want to edit this file change the template there. +// This file only holds the template that is used to generate a default .tools.toml. +//This file is generated by the build.rs script. If you want to edit this file change the template +//there. pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version 0.1.0 From 460d0212784d7f88d8b132f8eaa22a067874f748 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 16:19:45 +0200 Subject: [PATCH 03/25] fixed formatting --- build.rs | 6 +++--- src/config/template.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/build.rs b/build.rs index e7a5aed..8b3706b 100644 --- a/build.rs +++ b/build.rs @@ -50,9 +50,9 @@ fn format_template() -> String { let current_version = std::env::var("CARGO_PKG_VERSION").unwrap(); format!( - r####"// This file only holds the template that is used to generate a default .tools.toml. -//This file is generated by the build.rs script. If you want to edit this file change the template -//there. + r####"/// This file only holds the template that is used to generate a default .tools.toml. +// This file is generated by the build.rs script. If you want to edit this file change the template +// there. pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version {} {}"##; diff --git a/src/config/template.rs b/src/config/template.rs index c552a17..ddf479a 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -1,6 +1,6 @@ -// This file only holds the template that is used to generate a default .tools.toml. -//This file is generated by the build.rs script. If you want to edit this file change the template -//there. +/// This file only holds the template that is used to generate a default .tools.toml. +// This file is generated by the build.rs script. If you want to edit this file change the template +// there. pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version 0.1.0 From 3e5d4f7807f8bbbec7752733e215144688d03dbe Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 16:20:43 +0200 Subject: [PATCH 04/25] fixed more formatting issues --- build.rs | 2 +- src/config/template.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 8b3706b..53d05ab 100644 --- a/build.rs +++ b/build.rs @@ -54,7 +54,7 @@ fn format_template() -> String { // This file is generated by the build.rs script. If you want to edit this file change the template // there. -pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version {} +pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for version {} {}"##; "####, current_version, TEMPLATE_TEMPLATE diff --git a/src/config/template.rs b/src/config/template.rs index ddf479a..0263f0d 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -2,7 +2,7 @@ // This file is generated by the build.rs script. If you want to edit this file change the template // there. -pub const CONFIG_TEMPLATE: &str = r##"#This config file was generated for version 0.1.0 +pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for version 0.1.0 # # tool-sync default configuration file # https://github.com/chshersh/tool-sync From f7f5c57891ea59e285d6f884dbfebfb3e20cd5fb Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 16:22:09 +0200 Subject: [PATCH 05/25] fixed yet more formatting issues --- build.rs | 2 +- src/config/template.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 53d05ab..6dc5883 100644 --- a/build.rs +++ b/build.rs @@ -55,7 +55,7 @@ fn format_template() -> String { // there. pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for version {} -{}"##; +#{}"##; "####, current_version, TEMPLATE_TEMPLATE ) diff --git a/src/config/template.rs b/src/config/template.rs index 0263f0d..d581031 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -3,7 +3,7 @@ // there. pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for version 0.1.0 - +# # # tool-sync default configuration file # https://github.com/chshersh/tool-sync # This file was automatically generated by tool-sync From 243eb56a1d5e12e306c737279c3ccb1fbeb13d11 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 16:30:10 +0200 Subject: [PATCH 06/25] fixed variable name --- build.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.rs b/build.rs index 6dc5883..c85376a 100644 --- a/build.rs +++ b/build.rs @@ -40,8 +40,8 @@ const TEMPLATE_TEMPLATE: &str = r##" "##; fn main() -> Result<()> { - let s = format_template(); - fs::write("src/config/template.rs", s)?; + let template_content = format_template(); + fs::write("src/config/template.rs", template_content)?; Ok(()) } From 551706b6c512b220a0cdb99f63f9b288869c21eb Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 19:12:08 +0200 Subject: [PATCH 07/25] changed back template and incorporated a version --- build.rs | 62 ------------------------------------------ src/config/template.rs | 26 ++++++++++-------- 2 files changed, 14 insertions(+), 74 deletions(-) delete mode 100644 build.rs diff --git a/build.rs b/build.rs deleted file mode 100644 index c85376a..0000000 --- a/build.rs +++ /dev/null @@ -1,62 +0,0 @@ -use std::fs; -use std::io::Result; - -const TEMPLATE_TEMPLATE: &str = r##" -# # tool-sync default configuration file -# https://github.com/chshersh/tool-sync -# This file was automatically generated by tool-sync -##################################################### -# -# -# store_directory = "$HOME/.local/bin" -# -# tool-sync provides native support for some of the tools without the need to configure them -# Uncomment the tools you want to have them -# -# [bat] -# [difftastic] -# [fd] -# [ripgrep] -# -# To add configuration for other tools these are the config options: -# [ripgrep] -# owner = "BurntSushi" -# repo = "ripgrep" -# exe_name = "rg" -# -# # Uncomment to download a specific version or tag. -# # Without this tag latest will be used -# # tag = "13.0.0" -# -# -# Asset name to download on linux OSes -# asset_name.linux = "x86_64-unknown-linux-musl" -# -# uncomment if you want to install on macOS as well -# asset_name.macos = "apple-darwin" -# -# uncomment if you want to install on Windows as well -# asset_name.windows = "x86_64-pc-windows-msvc" -"##; - -fn main() -> Result<()> { - let template_content = format_template(); - fs::write("src/config/template.rs", template_content)?; - - Ok(()) -} - -fn format_template() -> String { - let current_version = std::env::var("CARGO_PKG_VERSION").unwrap(); - - format!( - r####"/// This file only holds the template that is used to generate a default .tools.toml. -// This file is generated by the build.rs script. If you want to edit this file change the template -// there. - -pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for version {} -#{}"##; -"####, - current_version, TEMPLATE_TEMPLATE - ) -} diff --git a/src/config/template.rs b/src/config/template.rs index d581031..adf08b4 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -1,8 +1,8 @@ /// This file only holds the template that is used to generate a default .tools.toml. -// This file is generated by the build.rs script. If you want to edit this file change the template -// there. -pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for version 0.1.0 +pub fn config_template() -> String { + format!( + "# This config file was generated for version {version} # # # tool-sync default configuration file # https://github.com/chshersh/tool-sync @@ -10,7 +10,7 @@ pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for versi ##################################################### # # -# store_directory = "$HOME/.local/bin" +# store_directory = \"$HOME/.local/bin\" # # tool-sync provides native support for some of the tools without the need to configure them # Uncomment the tools you want to have them @@ -22,21 +22,23 @@ pub const CONFIG_TEMPLATE: &str = r##"# This config file was generated for versi # # To add configuration for other tools these are the config options: # [ripgrep] -# owner = "BurntSushi" -# repo = "ripgrep" -# exe_name = "rg" +# owner = \"BurntSushi\" +# repo = \"ripgrep\" +# exe_name = \"rg\" # # # Uncomment to download a specific version or tag. # # Without this tag latest will be used -# # tag = "13.0.0" +# # tag = \"13.0.0\" # # # Asset name to download on linux OSes -# asset_name.linux = "x86_64-unknown-linux-musl" +# asset_name.linux = \"x86_64-unknown-linux-musl\" # # uncomment if you want to install on macOS as well -# asset_name.macos = "apple-darwin" +# asset_name.macos = \"apple-darwin\" # # uncomment if you want to install on Windows as well -# asset_name.windows = "x86_64-pc-windows-msvc" -"##; +# asset_name.windows = \"x86_64-pc-windows-msvc\"", + version = env!("CARGO_PKG_VERSION") + ) +} From d5196ac5c7ff255833cf1a9daf16314b35ef2df4 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 19:12:47 +0200 Subject: [PATCH 08/25] converted match statement to a hashmap --- src/lib.rs | 2 +- src/sync/db.rs | 66 +++++++++++++++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 88d0544..0541d98 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,5 +52,5 @@ fn resolve_config_path(config_path: Option) -> PathBuf { } fn generate_config() { - println!("{}", template::CONFIG_TEMPLATE); + println!("{}", template::config_template()); } diff --git a/src/sync/db.rs b/src/sync/db.rs index b805674..aea3ee0 100644 --- a/src/sync/db.rs +++ b/src/sync/db.rs @@ -1,10 +1,20 @@ +use std::collections::BTreeMap; + use crate::model::asset_name::AssetName; use crate::model::tool::{ToolInfo, ToolInfoTag}; /// Get info about known tools from a hardcoded database pub fn lookup_tool(tool_name: &str) -> Option { - match tool_name { - "bat" => Some(ToolInfo { + let mut known_db = build_db(); + known_db.get_mut(tool_name).unwrap().take() +} + +fn build_db() -> BTreeMap> { + let mut tools: BTreeMap> = BTreeMap::new(); + + tools.insert( + "bat".into(), + Some(ToolInfo { owner: "sharkdp".to_string(), repo: "bat".to_string(), exe_name: "bat".to_string(), @@ -15,7 +25,10 @@ pub fn lookup_tool(tool_name: &str) -> Option { }, tag: ToolInfoTag::Latest, }), - "difftastic" => Some(ToolInfo { + ); + tools.insert( + "difftastic".into(), + Some(ToolInfo { owner: "Wilfred".to_string(), repo: "difftastic".to_string(), exe_name: "difft".to_string(), @@ -26,7 +39,10 @@ pub fn lookup_tool(tool_name: &str) -> Option { }, tag: ToolInfoTag::Latest, }), - "exa" => Some(ToolInfo { + ); + tools.insert( + "exa".into(), + Some(ToolInfo { owner: "ogham".to_string(), repo: "exa".to_string(), exe_name: "exa".to_string(), @@ -37,7 +53,10 @@ pub fn lookup_tool(tool_name: &str) -> Option { }, tag: ToolInfoTag::Latest, }), - "fd" => Some(ToolInfo { + ); + tools.insert( + "fd".into(), + Some(ToolInfo { owner: "sharkdp".to_string(), repo: "fd".to_string(), exe_name: "fd".to_string(), @@ -48,7 +67,10 @@ pub fn lookup_tool(tool_name: &str) -> Option { }, tag: ToolInfoTag::Latest, }), - "ripgrep" => Some(ToolInfo { + ); + tools.insert( + "ripgrep".into(), + Some(ToolInfo { owner: "BurntSushi".to_string(), repo: "ripgrep".to_string(), exe_name: "rg".to_string(), @@ -59,7 +81,10 @@ pub fn lookup_tool(tool_name: &str) -> Option { }, tag: ToolInfoTag::Latest, }), - "tool-sync" => Some(ToolInfo { + ); + tools.insert( + "tool-sync".into(), + Some(ToolInfo { owner: "chshersh".to_string(), repo: "tool-sync".to_string(), exe_name: "tool".to_string(), @@ -70,17 +95,18 @@ pub fn lookup_tool(tool_name: &str) -> Option { }, tag: ToolInfoTag::Latest, }), - // "tokei" => Some(ToolInfo { - // owner: "XAMPPRocky".to_string(), - // repo: "tokei".to_string(), - // exe_name: "tokei".to_string(), - // asset_name: AssetName { - // linux: Some("x86_64-unknown-linux-musl".to_string()), - // macos: Some("apple-darwin".to_string()), - // windows: Some("x86_64-pc-windows-msvc".to_string()), - // } - // tag: ToolInfoTag::Latest, - // }), - _ => None, - } + ); + // tools.insert("tokei", Some(ToolInfo { + // owner: "XAMPPRocky".to_string(), + // repo: "tokei".to_string(), + // exe_name: "tokei".to_string(), + // asset_name: AssetName { + // linux: Some("x86_64-unknown-linux-musl".to_string()), + // macos: Some("apple-darwin".to_string()), + // windows: Some("x86_64-pc-windows-msvc".to_string()), + // } + // tag: ToolInfoTag::Latest, + // })); + // + tools } From ea0bd5077385c7a6ad979d0c99216a61838a75ae Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sat, 10 Sep 2022 19:19:41 +0200 Subject: [PATCH 09/25] fixed unwrapping issue --- src/sync/db.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sync/db.rs b/src/sync/db.rs index aea3ee0..e788150 100644 --- a/src/sync/db.rs +++ b/src/sync/db.rs @@ -6,7 +6,10 @@ use crate::model::tool::{ToolInfo, ToolInfoTag}; /// Get info about known tools from a hardcoded database pub fn lookup_tool(tool_name: &str) -> Option { let mut known_db = build_db(); - known_db.get_mut(tool_name).unwrap().take() + match known_db.get_mut(tool_name) { + Some(tool_info) => tool_info.take(), + None => None, + } } fn build_db() -> BTreeMap> { From 3881ae7caa0c430b2e878da6a305ca9b65f56d32 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 11 Sep 2022 12:40:03 +0200 Subject: [PATCH 10/25] made the template a string literal so there is no need for escaping " characters --- src/config/template.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/config/template.rs b/src/config/template.rs index adf08b4..f4f855b 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -2,7 +2,7 @@ pub fn config_template() -> String { format!( - "# This config file was generated for version {version} + r###"# This config file was generated for version {version} # # # tool-sync default configuration file # https://github.com/chshersh/tool-sync @@ -10,7 +10,7 @@ pub fn config_template() -> String { ##################################################### # # -# store_directory = \"$HOME/.local/bin\" +# store_directory = "$HOME/.local/bin" # # tool-sync provides native support for some of the tools without the need to configure them # Uncomment the tools you want to have them @@ -22,23 +22,23 @@ pub fn config_template() -> String { # # To add configuration for other tools these are the config options: # [ripgrep] -# owner = \"BurntSushi\" -# repo = \"ripgrep\" -# exe_name = \"rg\" +# owner = "BurntSushi" +# repo = "ripgrep" +# exe_name = "rg" # # # Uncomment to download a specific version or tag. # # Without this tag latest will be used -# # tag = \"13.0.0\" +# # tag = "13.0.0" # # # Asset name to download on linux OSes -# asset_name.linux = \"x86_64-unknown-linux-musl\" +# asset_name.linux = "x86_64-unknown-linux-musl" # # uncomment if you want to install on macOS as well -# asset_name.macos = \"apple-darwin\" +# asset_name.macos = "apple-darwin" # # uncomment if you want to install on Windows as well -# asset_name.windows = \"x86_64-pc-windows-msvc\"", +# asset_name.windows = "x86_64-pc-windows-msvc""###, version = env!("CARGO_PKG_VERSION") ) } From 4ffaae6b56aa4780193a0e04784c10ad8b7c13fd Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 11 Sep 2022 15:13:15 +0200 Subject: [PATCH 11/25] added default-config output test --- .github/workflows/ci.yml | 11 +++++++++++ tests/default-config.toml | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/default-config.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4b8f863..b1691f9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,6 +84,17 @@ jobs: # disabled because of: https://github.com/alexcrichton/tar-rs/issues/295 # if [[ ! -x $SYNC_DIR/tokei ]]; then echo "error on: tokei"; false; fi + - if: matrix.os != 'windows-latest' + name: "Characteristics test: [unix] [default-config]" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + cargo run -- default-config > tools.toml + + diff tools.toml tests/default-config.toml + + if [[ $? == 1 ]]; then diff tools.toml tests/default-config.toml; false; fi + - if: matrix.os == 'windows-latest' name: "Integration test: [windows] [full-database]" env: diff --git a/tests/default-config.toml b/tests/default-config.toml new file mode 100644 index 0000000..af2c491 --- /dev/null +++ b/tests/default-config.toml @@ -0,0 +1,37 @@ +# This config file was generated for version 0.1.0 +# +# # tool-sync default configuration file +# https://github.com/chshersh/tool-sync +# This file was automatically generated by tool-sync +##################################################### +# +# +# store_directory = "$HOME/.local/bin" +# +# tool-sync provides native support for some of the tools without the need to configure them +# Uncomment the tools you want to have them +# +# [bat] +# [difftastic] +# [fd] +# [ripgrep] +# +# To add configuration for other tools these are the config options: +# [ripgrep] +# owner = "BurntSushi" +# repo = "ripgrep" +# exe_name = "rg" +# +# # Uncomment to download a specific version or tag. +# # Without this tag latest will be used +# # tag = "13.0.0" +# +# +# Asset name to download on linux OSes +# asset_name.linux = "x86_64-unknown-linux-musl" +# +# uncomment if you want to install on macOS as well +# asset_name.macos = "apple-darwin" +# +# uncomment if you want to install on Windows as well +# asset_name.windows = "x86_64-pc-windows-msvc" From 6e31ff8c17ea8ae054ea7138490274fef47e3460 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 11 Sep 2022 15:18:24 +0200 Subject: [PATCH 12/25] This commit should fail in ci --- src/config/template.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config/template.rs b/src/config/template.rs index f4f855b..ab0e586 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -9,6 +9,7 @@ pub fn config_template() -> String { # This file was automatically generated by tool-sync ##################################################### # + # # store_directory = "$HOME/.local/bin" # From 4b2320e954d56e289d5d4903a50fde3ed5c0016a Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 11 Sep 2022 15:22:54 +0200 Subject: [PATCH 13/25] corrected typo in test name and added more output when test fails --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b1691f9..912f831 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -85,7 +85,7 @@ jobs: # if [[ ! -x $SYNC_DIR/tokei ]]; then echo "error on: tokei"; false; fi - if: matrix.os != 'windows-latest' - name: "Characteristics test: [unix] [default-config]" + name: "Characterization test: [unix] [default-config]" env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | @@ -93,7 +93,7 @@ jobs: diff tools.toml tests/default-config.toml - if [[ $? == 1 ]]; then diff tools.toml tests/default-config.toml; false; fi + if [[ $? == 1 ]]; then cat tools.toml && cat tests/default-config.toml; false; fi - if: matrix.os == 'windows-latest' name: "Integration test: [windows] [full-database]" From 10a5b708812425c4db324858c5240c0db6dfd1de Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 11 Sep 2022 15:30:06 +0200 Subject: [PATCH 14/25] removed error to make ci pass again --- .github/workflows/ci.yml | 2 +- src/config/template.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 912f831..adc939c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -93,7 +93,7 @@ jobs: diff tools.toml tests/default-config.toml - if [[ $? == 1 ]]; then cat tools.toml && cat tests/default-config.toml; false; fi + if [[ $? == 1 ]]; then cat tools.toml; false; fi - if: matrix.os == 'windows-latest' name: "Integration test: [windows] [full-database]" diff --git a/src/config/template.rs b/src/config/template.rs index ab0e586..f4f855b 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -9,7 +9,6 @@ pub fn config_template() -> String { # This file was automatically generated by tool-sync ##################################################### # - # # store_directory = "$HOME/.local/bin" # From 62fef0e5f4138b9ad9c66891024cd3f0d4fe210a Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Sun, 11 Sep 2022 17:09:59 +0200 Subject: [PATCH 15/25] added more tests to increase code coverage --- src/config/toml.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/config/toml.rs b/src/config/toml.rs index 41aa369..44cfe99 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -103,6 +103,89 @@ fn str_by_key(table: &Map, key: &str) -> Option { mod tests { use super::*; + #[test] + fn test_toml_error_display_io() { + let toml_error = TomlError::IO(String::from("some file error!")); + + assert_eq!( + String::from("[IO Error] some file error!"), + toml_error.display() + ); + } + + #[test] + fn test_toml_error_display_parse() { + //some setup for this test + use serde::Deserialize; + + #[derive(Deserialize)] + struct BrokenToml {} + + let broken_toml_str: String = "broken toml".into(); + match toml::de::from_str::(&broken_toml_str) { + Err(error) => { + let toml_error = TomlError::Parse(error); + assert_eq!( + String::from( + "[Parsing Error] expected an equals, found an identifier at line 1 column 8" + ), + toml_error.display() + ); + } + Ok(_) => unreachable!(), + }; + } + + #[test] + fn test_toml_error_display_decode() { + let toml_error = TomlError::Decode; + assert_eq!(String::from("[Decode Error]"), toml_error.display()); + } + + #[test] + fn test_parse_file_correct_output() { + let test_config_path = PathBuf::from("tests/full-database.toml"); + let config = parse_file(&test_config_path).expect("This should not fail"); + + assert_eq!(String::from("full-database"), config.store_directory); + } + + #[test] + fn test_parse_file_error() { + let test_config_path = PathBuf::from("src/main.rs"); + #[allow(unused_assignments)] + let mut final_assert = false; + + match parse_file(&test_config_path) { + Ok(_) => { + unreachable!() + } + Err(_) => { + final_assert = true; + } + }; + assert!(final_assert); + } + + #[test] + fn test_parse_file_passing_tools_read() { + let test_config_path = PathBuf::from("tests/full-database.toml"); + let config = parse_file(&test_config_path).expect("This should not fail"); + + // A BTree is sorted so the keys are also collected in order + let tools_vec: Vec = config.tools.keys().cloned().collect(); + let correct_vec: Vec = vec![ + "bat".into(), + "difftastic".into(), + "exa".into(), + "fd".into(), + "ripgrep".into(), + "tool-sync".into(), + ]; + + assert_eq!(correct_vec, tools_vec); + } + #[test] fn empty_file() { let toml = ""; From 8b128504e80ddc5e7b6643d43b47a0e56aad0573 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Tue, 13 Sep 2022 17:33:21 +0200 Subject: [PATCH 16/25] added a generated tools block so the default config that is generated always includes all tools defined in src/sync/db.rs --- src/config/template.rs | 20 ++++++++++++++------ src/sync.rs | 2 +- src/sync/db.rs | 2 +- tests/default-config.toml | 2 ++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/config/template.rs b/src/config/template.rs index f4f855b..49ebc0a 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -1,6 +1,18 @@ /// This file only holds the template that is used to generate a default .tools.toml. +use std::fmt::Write; + +use crate::sync::db::build_db; pub fn config_template() -> String { + let mut tools: String = String::new(); + for tool in build_db().keys().cloned().collect::>() { + if let Err(e) = writeln!(tools, "# [{}]", tool) { + panic!("{}", e); + }; + } + // adding another hash to fil a new line before the next block + tools.push('#'); + format!( r###"# This config file was generated for version {version} # @@ -15,11 +27,7 @@ pub fn config_template() -> String { # tool-sync provides native support for some of the tools without the need to configure them # Uncomment the tools you want to have them # -# [bat] -# [difftastic] -# [fd] -# [ripgrep] -# +{tools} # To add configuration for other tools these are the config options: # [ripgrep] # owner = "BurntSushi" @@ -39,6 +47,6 @@ pub fn config_template() -> String { # # uncomment if you want to install on Windows as well # asset_name.windows = "x86_64-pc-windows-msvc""###, - version = env!("CARGO_PKG_VERSION") + version = env!("CARGO_PKG_VERSION"), ) } diff --git a/src/sync.rs b/src/sync.rs index 2b30de0..b3b5aa0 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,6 +1,6 @@ mod archive; mod configure; -mod db; +pub mod db; mod download; mod install; mod prefetch; diff --git a/src/sync/db.rs b/src/sync/db.rs index e788150..602e85f 100644 --- a/src/sync/db.rs +++ b/src/sync/db.rs @@ -12,7 +12,7 @@ pub fn lookup_tool(tool_name: &str) -> Option { } } -fn build_db() -> BTreeMap> { +pub fn build_db() -> BTreeMap> { let mut tools: BTreeMap> = BTreeMap::new(); tools.insert( diff --git a/tests/default-config.toml b/tests/default-config.toml index af2c491..0b3fbe5 100644 --- a/tests/default-config.toml +++ b/tests/default-config.toml @@ -13,8 +13,10 @@ # # [bat] # [difftastic] +# [exa] # [fd] # [ripgrep] +# [tool-sync] # # To add configuration for other tools these are the config options: # [ripgrep] From 093e667e559cece03acca7aa776eae40c5c5fd06 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Tue, 13 Sep 2022 19:22:36 +0200 Subject: [PATCH 17/25] converted simple panic to panic_suggest_issue from src/err.rs --- src/config/template.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/template.rs b/src/config/template.rs index 49ebc0a..c9b23fe 100644 --- a/src/config/template.rs +++ b/src/config/template.rs @@ -7,7 +7,7 @@ pub fn config_template() -> String { let mut tools: String = String::new(); for tool in build_db().keys().cloned().collect::>() { if let Err(e) = writeln!(tools, "# [{}]", tool) { - panic!("{}", e); + crate::err::abort_suggest_issue(&format!("{}", e)); }; } // adding another hash to fil a new line before the next block From eaf6924de0a0ed4fbff09b230b0eda8ba0122d7c Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Wed, 14 Sep 2022 08:48:00 +0200 Subject: [PATCH 18/25] changed test only test the function itself instead of only the error --- src/config/toml.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/config/toml.rs b/src/config/toml.rs index 44cfe99..97e113b 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -115,21 +115,14 @@ mod tests { #[test] fn test_toml_error_display_parse() { - //some setup for this test - use serde::Deserialize; - - #[derive(Deserialize)] - struct BrokenToml {} - let broken_toml_str: String = "broken toml".into(); - match toml::de::from_str::(&broken_toml_str) { + match parse_string(&broken_toml_str) { Err(error) => { - let toml_error = TomlError::Parse(error); assert_eq!( String::from( "[Parsing Error] expected an equals, found an identifier at line 1 column 8" ), - toml_error.display() + error.display() ); } Ok(_) => unreachable!(), From 2870cb49fcf13eb37798fc86e8845df5a0240495 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Wed, 14 Sep 2022 08:52:10 +0200 Subject: [PATCH 19/25] added more clarity on a test when its function succeeds when it should fail --- src/config/toml.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/config/toml.rs b/src/config/toml.rs index 97e113b..cace6b4 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -146,18 +146,14 @@ mod tests { #[test] fn test_parse_file_error() { let test_config_path = PathBuf::from("src/main.rs"); - #[allow(unused_assignments)] - let mut final_assert = false; - match parse_file(&test_config_path) { Ok(_) => { - unreachable!() + assert!(false, "Unexpected succces") } Err(_) => { - final_assert = true; + assert!(true, "Exepected a parsing error") } }; - assert!(final_assert); } #[test] From c418edc177f8598a371367638e5f0770102fa146 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Wed, 14 Sep 2022 08:55:02 +0200 Subject: [PATCH 20/25] removed test that is already covered in characterization test --- src/config/toml.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/config/toml.rs b/src/config/toml.rs index cace6b4..e8608c7 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -156,25 +156,6 @@ mod tests { }; } - #[test] - fn test_parse_file_passing_tools_read() { - let test_config_path = PathBuf::from("tests/full-database.toml"); - let config = parse_file(&test_config_path).expect("This should not fail"); - - // A BTree is sorted so the keys are also collected in order - let tools_vec: Vec = config.tools.keys().cloned().collect(); - let correct_vec: Vec = vec![ - "bat".into(), - "difftastic".into(), - "exa".into(), - "fd".into(), - "ripgrep".into(), - "tool-sync".into(), - ]; - - assert_eq!(correct_vec, tools_vec); - } - #[test] fn empty_file() { let toml = ""; From 0d7e91aaaec5a9cc7a5a767b502894e42b0fde0f Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Wed, 14 Sep 2022 11:25:49 +0200 Subject: [PATCH 21/25] removed Option from the btree value type --- src/sync/db.rs | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/sync/db.rs b/src/sync/db.rs index 602e85f..231202c 100644 --- a/src/sync/db.rs +++ b/src/sync/db.rs @@ -6,18 +6,15 @@ use crate::model::tool::{ToolInfo, ToolInfoTag}; /// Get info about known tools from a hardcoded database pub fn lookup_tool(tool_name: &str) -> Option { let mut known_db = build_db(); - match known_db.get_mut(tool_name) { - Some(tool_info) => tool_info.take(), - None => None, - } + known_db.remove(tool_name) } -pub fn build_db() -> BTreeMap> { - let mut tools: BTreeMap> = BTreeMap::new(); +pub fn build_db() -> BTreeMap { + let mut tools: BTreeMap = BTreeMap::new(); tools.insert( "bat".into(), - Some(ToolInfo { + ToolInfo { owner: "sharkdp".to_string(), repo: "bat".to_string(), exe_name: "bat".to_string(), @@ -27,11 +24,11 @@ pub fn build_db() -> BTreeMap> { windows: Some("x86_64-pc-windows-msvc".to_string()), }, tag: ToolInfoTag::Latest, - }), + }, ); tools.insert( "difftastic".into(), - Some(ToolInfo { + ToolInfo { owner: "Wilfred".to_string(), repo: "difftastic".to_string(), exe_name: "difft".to_string(), @@ -41,11 +38,11 @@ pub fn build_db() -> BTreeMap> { windows: Some("x86_64-pc-windows-msvc".to_string()), }, tag: ToolInfoTag::Latest, - }), + }, ); tools.insert( "exa".into(), - Some(ToolInfo { + ToolInfo { owner: "ogham".to_string(), repo: "exa".to_string(), exe_name: "exa".to_string(), @@ -55,11 +52,11 @@ pub fn build_db() -> BTreeMap> { windows: None, }, tag: ToolInfoTag::Latest, - }), + }, ); tools.insert( "fd".into(), - Some(ToolInfo { + ToolInfo { owner: "sharkdp".to_string(), repo: "fd".to_string(), exe_name: "fd".to_string(), @@ -69,11 +66,11 @@ pub fn build_db() -> BTreeMap> { windows: Some("x86_64-pc-windows-msvc".to_string()), }, tag: ToolInfoTag::Latest, - }), + }, ); tools.insert( "ripgrep".into(), - Some(ToolInfo { + ToolInfo { owner: "BurntSushi".to_string(), repo: "ripgrep".to_string(), exe_name: "rg".to_string(), @@ -83,11 +80,11 @@ pub fn build_db() -> BTreeMap> { windows: Some("x86_64-pc-windows-msvc".to_string()), }, tag: ToolInfoTag::Latest, - }), + }, ); tools.insert( "tool-sync".into(), - Some(ToolInfo { + ToolInfo { owner: "chshersh".to_string(), repo: "tool-sync".to_string(), exe_name: "tool".to_string(), @@ -97,9 +94,9 @@ pub fn build_db() -> BTreeMap> { windows: Some("x86_64-pc-windows-msvc".to_string()), }, tag: ToolInfoTag::Latest, - }), + }, ); - // tools.insert("tokei", Some(ToolInfo { + // tools.insert("tokei", ToolInfo { // owner: "XAMPPRocky".to_string(), // repo: "tokei".to_string(), // exe_name: "tokei".to_string(), From 03cd37297ffced016f1cb50a8398e0440a338066 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Wed, 14 Sep 2022 11:32:12 +0200 Subject: [PATCH 22/25] changed output of default-config command test --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a427d16..3fd2273 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -98,7 +98,7 @@ jobs: diff tools.toml tests/default-config.toml - if [[ $? == 1 ]]; then cat tools.toml; false; fi + if [[ $? == 1 ]]; then tools.toml tests/default-config.toml; false; fi - if: matrix.os == 'windows-latest' name: "Integration test: [windows] [full-database]" From 6e1d5edc470c8e8f0461c8f45d02234f7dbb9735 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Wed, 14 Sep 2022 16:41:50 +0200 Subject: [PATCH 23/25] wrapped parse_file in a catch unwind so the test actually checks if the parsing fails before it asserts --- src/config/toml.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/config/toml.rs b/src/config/toml.rs index e8608c7..cbbd35f 100644 --- a/src/config/toml.rs +++ b/src/config/toml.rs @@ -137,10 +137,14 @@ mod tests { #[test] fn test_parse_file_correct_output() { - let test_config_path = PathBuf::from("tests/full-database.toml"); - let config = parse_file(&test_config_path).expect("This should not fail"); + let result = std::panic::catch_unwind(|| { + let test_config_path = PathBuf::from("tests/full-database.toml"); + parse_file(&test_config_path).expect("This should not fail") + }); - assert_eq!(String::from("full-database"), config.store_directory); + if let Ok(config) = result { + assert_eq!(String::from("full-database"), config.store_directory); + }; } #[test] From 07c9d0e365b987f3d772147f04934d24f0e61a83 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen <45570310+MitchellBerend@users.noreply.github.com> Date: Wed, 14 Sep 2022 18:13:48 +0200 Subject: [PATCH 24/25] Fixed typo Co-authored-by: Dmitrii Kovanikov --- .github/workflows/ci.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3fd2273..7925b6a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -98,7 +98,6 @@ jobs: diff tools.toml tests/default-config.toml - if [[ $? == 1 ]]; then tools.toml tests/default-config.toml; false; fi - if: matrix.os == 'windows-latest' name: "Integration test: [windows] [full-database]" From 9056e817efd42ee60c39b4867ea8d7dd215e69f7 Mon Sep 17 00:00:00 2001 From: Mitchell Berendhuysen Date: Wed, 14 Sep 2022 18:16:19 +0200 Subject: [PATCH 25/25] fixed actual typo instead of removing it --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7925b6a..6dadbf9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -98,6 +98,7 @@ jobs: diff tools.toml tests/default-config.toml + if [[ $? == 1 ]]; then diff tools.toml tests/default-config.toml; false; fi - if: matrix.os == 'windows-latest' name: "Integration test: [windows] [full-database]"