Skip to content

Commit 68aecc9

Browse files
committed
Update config handling and add e2e tests
- Add tests for local list get and set of OpenAI model - Add `get_config_path` function for retrieving either local or user config path - Update `delete` and `set` functions to use `get_config_path` function - Add async-std dependency and update anyhow and async-trait versions [e2e/test_config_local_list_get_set.sh] - Add tests for local list get and set of OpenAI model - Assert that local config set and delete fail when not using --local flag - Assert that local config get returns the correct value after setting and deleting with --local flag [src/actions/config.rs] - Add `async_std::path::Path` to the imports - Change the `get_config_path` function to handle both local and user config paths - Add `get_config_path` function for retrieving either local or user config path - Add error handling to the `delete` and `set` functions for when no config path is found - Update the `delete` and `set` functions to use the `get_config_path` function [Cargo.toml] - Add async-std dependency - Update anyhow and async-trait versions
1 parent 3a8264b commit 68aecc9

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ path = "src/main.rs"
1818

1919
[dependencies]
2020
anyhow = "1.0.69"
21+
async-std = "1.12.0"
2122
async-trait = "0.1.64"
2223
clap = { version = "4.1.6", features = ["derive"] }
2324
colored = "2.0.0"

e2e/test_config_local_list_get_set.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,35 @@ export TEMPDIR=$(mktemp -d)
2424
# assert is default
2525
)
2626
rm -rf "${TEMPDIR}"
27+
28+
29+
export TEMPDIR=$(mktemp -d)
30+
(
31+
cd "${TEMPDIR}"
32+
33+
gptcommit config list
34+
# assert is valid TOML
35+
36+
gptcommit config get openai.model
37+
# assert default = text-davinci-003
38+
set +e
39+
gptcommit config set --local openai.model foo
40+
# TODO assert output
41+
test $? -ne 0 || exit $?
42+
set -e
43+
gptcommit config set openai.model bar
44+
gptcommit config get openai.model
45+
# assert is foo
46+
47+
gptcommit config delete openai.model
48+
gptcommit config get openai.model
49+
# assert still is foo
50+
set +e
51+
gptcommit config delete --local openai.model
52+
# TODO assert output
53+
test $? -ne 0 || exit $?
54+
set -e
55+
gptcommit config get openai.model
56+
# assert is default
57+
)
58+
rm -rf "${TEMPDIR}"

src/actions/config.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{collections::VecDeque, fs, path::PathBuf};
22

3+
use async_std::path::Path;
34
use clap::{Args, Subcommand};
45
use toml::Value;
56

@@ -50,14 +51,26 @@ pub(crate) async fn main(settings: Settings, args: ConfigArgs) -> Result<()> {
5051
}
5152
}
5253

54+
fn get_config_path(local: bool) -> Result<PathBuf> {
55+
if local {
56+
if let Some(config_path) = get_local_config_path() {
57+
Ok(config_path)
58+
} else {
59+
bail!("No repo-local config found. Please run `git init` to create a repo first");
60+
}
61+
} else {
62+
if let Some(config_path) = get_user_config_path() {
63+
Ok(config_path)
64+
} else {
65+
bail!("No user config found.");
66+
}
67+
}
68+
}
69+
5370
async fn delete(_settings: Settings, full_key: String, local: bool) -> Result<()> {
5471
let settings = &Settings::from_clear(&full_key)?;
5572
let toml_string = toml::to_string_pretty(settings).unwrap();
56-
let config_path: PathBuf = if local {
57-
get_local_config_path().expect("Could not find repo-local config path")
58-
} else {
59-
get_user_config_path().expect("Could not find user config path")
60-
};
73+
let config_path = get_config_path(local)?;
6174
fs::write(&config_path, toml_string)?;
6275
println!("Cleared {full_key}");
6376
println!("Config saved to {}", config_path.display());
@@ -67,11 +80,7 @@ async fn delete(_settings: Settings, full_key: String, local: bool) -> Result<()
6780
async fn set(_settings: Settings, full_key: String, value: String, local: bool) -> Result<()> {
6881
let settings = &Settings::from_set_override(&full_key, &value)?;
6982
let toml_string = toml::to_string_pretty(settings).unwrap();
70-
let config_path: PathBuf = if local {
71-
get_local_config_path().expect("Could not find repo-local config path")
72-
} else {
73-
get_user_config_path().expect("Could not find user config path")
74-
};
83+
let config_path = get_config_path(local)?;
7584
fs::write(&config_path, toml_string)?;
7685
println!("{full_key} = {value}");
7786
println!("Config saved to {}", config_path.display());

0 commit comments

Comments
 (0)