Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#73] Adds build.rs to generate a default config template with tool version #78

Merged
merged 27 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e4a281f
adding cargo fmted files
MitchellBerend Sep 10, 2022
56910b3
Added comment back that was deleted accidentally
MitchellBerend Sep 10, 2022
460d021
fixed formatting
MitchellBerend Sep 10, 2022
3e5d4f7
fixed more formatting issues
MitchellBerend Sep 10, 2022
f7f5c57
fixed yet more formatting issues
MitchellBerend Sep 10, 2022
243eb56
fixed variable name
MitchellBerend Sep 10, 2022
551706b
changed back template and incorporated a version
MitchellBerend Sep 10, 2022
d5196ac
converted match statement to a hashmap
MitchellBerend Sep 10, 2022
ea0bd50
fixed unwrapping issue
MitchellBerend Sep 10, 2022
4c11c31
Merge branch 'main' into 73-add-version-to-config
MitchellBerend Sep 10, 2022
3881ae7
made the template a string literal so there is no need for escaping "…
MitchellBerend Sep 11, 2022
4ffaae6
added default-config output test
MitchellBerend Sep 11, 2022
6e31ff8
This commit should fail in ci
MitchellBerend Sep 11, 2022
4b2320e
corrected typo in test name and added more output when test fails
MitchellBerend Sep 11, 2022
10a5b70
removed error to make ci pass again
MitchellBerend Sep 11, 2022
62fef0e
added more tests to increase code coverage
MitchellBerend Sep 11, 2022
ae60e3b
Merge branch 'main' into 73-add-version-to-config
MitchellBerend Sep 13, 2022
8b12850
added a generated tools block so the default config that is generated…
MitchellBerend Sep 13, 2022
093e667
converted simple panic to panic_suggest_issue from src/err.rs
MitchellBerend Sep 13, 2022
eaf6924
changed test only test the function itself instead of only the error
MitchellBerend Sep 14, 2022
2870cb4
added more clarity on a test when its function succeeds when it shoul…
MitchellBerend Sep 14, 2022
c418edc
removed test that is already covered in characterization test
MitchellBerend Sep 14, 2022
0d7e91a
removed Option<ToolInfo> from the btree value type
MitchellBerend Sep 14, 2022
03cd372
changed output of default-config command test
MitchellBerend Sep 14, 2022
6e1d5ed
wrapped parse_file in a catch unwind so the test actually checks if t…
MitchellBerend Sep 14, 2022
07c9d0e
Fixed typo
MitchellBerend Sep 14, 2022
9056e81
fixed actual typo instead of removing it
MitchellBerend Sep 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,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: "Characterization 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 tools.toml tests/default-config.toml; false; fi
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved

- if: matrix.os == 'windows-latest'
name: "Integration test: [windows] [full-database]"
env:
Expand Down
30 changes: 22 additions & 8 deletions src/config/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
/// This file only holds the template that is used to generate a default .tools.toml.
use std::fmt::Write;

pub const CONFIG_TEMPLATE: &str = r##"# # tool-sync default configuration file
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::<Vec<String>>() {
if let Err(e) = writeln!(tools, "# [{}]", tool) {
crate::err::abort_suggest_issue(&format!("{}", e));
};
}
// adding another hash to fil a new line before the next block
tools.push('#');
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved

format!(
r###"# This config file was generated for version {version}
#
# # tool-sync default configuration file
# https://github.com/chshersh/tool-sync
# This file was automatically generated by tool-sync
#####################################################
Expand All @@ -11,11 +27,7 @@ pub const CONFIG_TEMPLATE: &str = r##"# # tool-sync default configuration file
# 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"
Expand All @@ -34,5 +46,7 @@ pub const CONFIG_TEMPLATE: &str = r##"# # tool-sync default configuration file
# 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"),
)
}
57 changes: 57 additions & 0 deletions src/config/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,63 @@ fn str_by_key(table: &Map<String, Value>, key: &str) -> Option<String> {
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()
);
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
fn test_toml_error_display_parse() {
let broken_toml_str: String = "broken toml".into();
match parse_string(&broken_toml_str) {
Err(error) => {
assert_eq!(
String::from(
"[Parsing Error] expected an equals, found an identifier at line 1 column 8"
),
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());
}
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved

#[test]
fn test_parse_file_correct_output() {
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")
});

if let Ok(config) = result {
assert_eq!(String::from("full-database"), config.store_directory);
};
}

#[test]
fn test_parse_file_error() {
let test_config_path = PathBuf::from("src/main.rs");
match parse_file(&test_config_path) {
Ok(_) => {
assert!(false, "Unexpected succces")
}
Err(_) => {
assert!(true, "Exepected a parsing error")
}
};
}

#[test]
fn empty_file() {
let toml = "";
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ fn resolve_config_path(config_path: Option<PathBuf>) -> PathBuf {
}

fn generate_config() {
println!("{}", template::CONFIG_TEMPLATE);
println!("{}", template::config_template());
}
2 changes: 1 addition & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod archive;
mod configure;
mod db;
pub mod db;
mod download;
mod install;
mod prefetch;
Expand Down
78 changes: 52 additions & 26 deletions src/sync/db.rs
Original file line number Diff line number Diff line change
@@ -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<ToolInfo> {
match tool_name {
"bat" => Some(ToolInfo {
let mut known_db = build_db();
MitchellBerend marked this conversation as resolved.
Show resolved Hide resolved
known_db.remove(tool_name)
}

pub fn build_db() -> BTreeMap<String, ToolInfo> {
let mut tools: BTreeMap<String, ToolInfo> = BTreeMap::new();

tools.insert(
"bat".into(),
ToolInfo {
owner: "sharkdp".to_string(),
repo: "bat".to_string(),
exe_name: "bat".to_string(),
Expand All @@ -14,8 +24,11 @@ pub fn lookup_tool(tool_name: &str) -> Option<ToolInfo> {
windows: Some("x86_64-pc-windows-msvc".to_string()),
},
tag: ToolInfoTag::Latest,
}),
"difftastic" => Some(ToolInfo {
},
);
tools.insert(
"difftastic".into(),
ToolInfo {
owner: "Wilfred".to_string(),
repo: "difftastic".to_string(),
exe_name: "difft".to_string(),
Expand All @@ -25,8 +38,11 @@ pub fn lookup_tool(tool_name: &str) -> Option<ToolInfo> {
windows: Some("x86_64-pc-windows-msvc".to_string()),
},
tag: ToolInfoTag::Latest,
}),
"exa" => Some(ToolInfo {
},
);
tools.insert(
"exa".into(),
ToolInfo {
owner: "ogham".to_string(),
repo: "exa".to_string(),
exe_name: "exa".to_string(),
Expand All @@ -36,8 +52,11 @@ pub fn lookup_tool(tool_name: &str) -> Option<ToolInfo> {
windows: None,
},
tag: ToolInfoTag::Latest,
}),
"fd" => Some(ToolInfo {
},
);
tools.insert(
"fd".into(),
ToolInfo {
owner: "sharkdp".to_string(),
repo: "fd".to_string(),
exe_name: "fd".to_string(),
Expand All @@ -47,8 +66,11 @@ pub fn lookup_tool(tool_name: &str) -> Option<ToolInfo> {
windows: Some("x86_64-pc-windows-msvc".to_string()),
},
tag: ToolInfoTag::Latest,
}),
"ripgrep" => Some(ToolInfo {
},
);
tools.insert(
"ripgrep".into(),
ToolInfo {
owner: "BurntSushi".to_string(),
repo: "ripgrep".to_string(),
exe_name: "rg".to_string(),
Expand All @@ -58,8 +80,11 @@ pub fn lookup_tool(tool_name: &str) -> Option<ToolInfo> {
windows: Some("x86_64-pc-windows-msvc".to_string()),
},
tag: ToolInfoTag::Latest,
}),
"tool-sync" => Some(ToolInfo {
},
);
tools.insert(
"tool-sync".into(),
ToolInfo {
owner: "chshersh".to_string(),
repo: "tool-sync".to_string(),
exe_name: "tool".to_string(),
Expand All @@ -69,18 +94,19 @@ pub fn lookup_tool(tool_name: &str) -> Option<ToolInfo> {
windows: Some("x86_64-pc-windows-msvc".to_string()),
},
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", 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
}
39 changes: 39 additions & 0 deletions tests/default-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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]
# [exa]
# [fd]
# [ripgrep]
# [tool-sync]
#
# 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"