Skip to content

Commit 0c409d2

Browse files
committed
further refactoring
1 parent 0574dfe commit 0c409d2

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

src/lib.rs

+29-26
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod list;
99
mod pixi_lock;
1010
mod read_remote;
1111

12-
use std::{io::Write, path::PathBuf};
12+
use std::{env, io::Write, path::PathBuf};
1313

1414
use cli::CondaDenyCliConfig;
1515
use colored::Colorize;
@@ -72,33 +72,37 @@ pub fn get_config_options(
7272
config: Option<String>,
7373
cli_config: CondaDenyCliConfig,
7474
) -> Result<CondaDenyConfig> {
75-
let (toml_config, toml_config_path): (CondaDenyTomlConfig, PathBuf) = if let Some(config_path) =
76-
config
77-
{
78-
(
79-
CondaDenyTomlConfig::from_path(config_path.as_str())
80-
.with_context(|| format!("Failed to parse config file {}", config_path))?,
81-
config_path.into(),
82-
)
75+
// if config provided, use config
76+
// else, try to load pixi.toml, then pyproject.toml and if nothing helps, use empty config
77+
let toml_config = if let Some(config_path) = config {
78+
CondaDenyTomlConfig::from_path(config_path.as_str())
79+
.with_context(|| format!("Failed to parse config file {}", config_path))?
8380
} else {
8481
match CondaDenyTomlConfig::from_path("pixi.toml")
8582
.with_context(|| "Failed to parse config file pixi.toml")
8683
{
8784
Ok(config) => {
8885
debug!("Successfully loaded config from pixi.toml");
89-
(config, "pixi.toml".into())
86+
config
9087
}
9188
Err(e) => {
9289
debug!(
9390
"Error parsing config file: pixi.toml: {}. Attempting to use pyproject.toml instead...",
9491
e
9592
);
96-
(
97-
CondaDenyTomlConfig::from_path("pyproject.toml")
98-
.context(e)
99-
.with_context(|| "Failed to parse config file pyproject.toml")?,
100-
"pyproject.toml".into(),
101-
)
93+
match CondaDenyTomlConfig::from_path("pyproject.toml")
94+
.context(e)
95+
.with_context(|| "Failed to parse config file pyproject.toml")
96+
{
97+
Ok(config) => config,
98+
Err(e) => {
99+
debug!(
100+
"Error parsing config file: pyproject.toml: {}. Using empty config instead...",
101+
e
102+
);
103+
CondaDenyTomlConfig::empty()
104+
}
105+
}
102106
}
103107
}
104108
};
@@ -121,10 +125,7 @@ pub fn get_config_options(
121125

122126
let lockfile_or_prefix = if lockfile.is_empty() && prefix.is_empty() {
123127
// test if pixi.lock exists next to config file, otherwise error
124-
let default_lockfile_path = toml_config_path
125-
.parent()
126-
.context("could not get parent of toml config path")?
127-
.join("pixi.lock");
128+
let default_lockfile_path = env::current_dir()?.join("pixi.lock");
128129
if !default_lockfile_path.is_file() {
129130
Err(anyhow::anyhow!("No lockfiles or conda prefixes provided"))
130131
} else {
@@ -194,8 +195,13 @@ pub fn get_config_options(
194195
}
195196
.unwrap_or(false);
196197

197-
let (safe_licenses, ignore_packages) =
198-
get_license_information_from_toml_config(toml_config)?;
198+
// osi and safe-licenses are mutually exclusive
199+
// todo: throw an error here instead
200+
let (safe_licenses, ignore_packages) = if osi {
201+
(vec![], vec![])
202+
} else {
203+
get_license_information_from_toml_config(toml_config)?
204+
};
199205

200206
CondaDenyConfig::Check(CondaDenyCheckConfig {
201207
lockfile_or_prefix,
@@ -222,10 +228,7 @@ pub fn get_config_options(
222228
// todo: duplicate code
223229
let lockfile_or_prefix = if lockfile.is_empty() && prefix.is_empty() {
224230
// test if pixi.lock exists next to config file, otherwise error
225-
let default_lockfile_path = toml_config_path
226-
.parent()
227-
.context("could not get parent of toml config path")?
228-
.join("pixi.lock");
231+
let default_lockfile_path = env::current_dir()?.join("pixi.lock");
229232
if !default_lockfile_path.is_file() {
230233
Err(anyhow::anyhow!("No lockfiles or conda prefixes provided"))
231234
} else {

tests/integration_tests.rs

+25-29
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::process::Command;
99

1010
#[fixture]
1111
fn list_config(
12-
#[default(PathBuf::from("examples/simple-python/pixi.toml"))] config: PathBuf,
12+
#[default(None)] config: Option<PathBuf>,
1313
#[default(None)] lockfile: Option<Vec<String>>,
1414
#[default(None)] prefix: Option<Vec<String>>,
1515
#[default(None)] platform: Option<Vec<String>>,
@@ -23,7 +23,7 @@ fn list_config(
2323
};
2424

2525
let config = get_config_options(
26-
Some(config.to_str().unwrap().into()), // todo: prettier
26+
config.map(|p| p.to_str().unwrap().to_string()), // todo: prettier
2727
cli,
2828
)
2929
.unwrap();
@@ -36,7 +36,7 @@ fn list_config(
3636

3737
#[fixture]
3838
fn check_config(
39-
#[default(PathBuf::from("examples/simple-python/pixi.toml"))] config: PathBuf,
39+
#[default(None)] config: Option<PathBuf>,
4040
#[default(None)] lockfile: Option<Vec<String>>,
4141
#[default(None)] prefix: Option<Vec<String>>,
4242
#[default(None)] platform: Option<Vec<String>>,
@@ -56,7 +56,7 @@ fn check_config(
5656
};
5757

5858
let config = get_config_options(
59-
Some(config.to_str().unwrap().into()), // todo: prettier
59+
config.map(|p| p.to_str().unwrap().to_string()), // todo: prettier
6060
cli,
6161
);
6262
let config = config.unwrap();
@@ -78,21 +78,13 @@ fn out() -> Vec<u8> {
7878
}
7979

8080
#[rstest]
81-
#[case("check")]
82-
#[case("list")]
83-
fn test_default_use_case(#[case] subcommand: &str) {
84-
let test_dir = Path::new("tests/test_end_to_end/test_default_use_case");
85-
86-
let mut command = Command::cargo_bin("conda-deny").unwrap();
87-
command.arg(subcommand).current_dir(test_dir);
88-
command.assert().success();
89-
}
90-
91-
#[rstest]
92-
#[case("check")]
93-
#[case("list")]
94-
fn test_default_use_case_pyproject(#[case] subcommand: &str) {
95-
let test_dir = Path::new("tests/test_end_to_end/test_default_use_case_pyproject");
81+
#[case("check", "test_default_use_case")]
82+
#[case("list", "test_default_use_case")]
83+
#[case("check", "test_default_use_case_pyproject")]
84+
#[case("list", "test_default_use_case_pyproject")]
85+
fn test_default_use_case_pyproject(#[case] subcommand: &str, #[case] test_name: &str) {
86+
let path_string = format!("tests/test_end_to_end/{}", test_name);
87+
let test_dir = Path::new(path_string.as_str());
9688

9789
let mut command = Command::cargo_bin("conda-deny").unwrap();
9890
command.arg(subcommand).current_dir(test_dir);
@@ -101,7 +93,7 @@ fn test_default_use_case_pyproject(#[case] subcommand: &str) {
10193

10294
#[rstest]
10395
fn test_remote_whitelist_check(
104-
#[with(PathBuf::from("tests/test_end_to_end/test_remote_whitelist/pixi.toml"))]
96+
#[with(Some(PathBuf::from("tests/test_end_to_end/test_remote_whitelist/pixi.toml")), Some(vec!["tests/test_end_to_end/test_remote_whitelist/pixi.lock".into()]))]
10597
check_config: CondaDenyCheckConfig,
10698
mut out: Vec<u8>,
10799
) {
@@ -112,7 +104,7 @@ fn test_remote_whitelist_check(
112104
#[rstest]
113105
fn test_remote_whitelist_list(
114106
#[with(
115-
PathBuf::from("tests/test_end_to_end/test_remote_whitelist/pixi.toml"),
107+
Some(PathBuf::from("tests/test_end_to_end/test_remote_whitelist/pixi.toml")),
116108
None,
117109
Some(vec!["tests/test_conda_prefixes/test-env".into()]),
118110
)]
@@ -127,8 +119,10 @@ fn test_remote_whitelist_list(
127119
#[rstest]
128120
fn test_multiple_whitelists_check(
129121
#[with(
130-
PathBuf::from("tests/test_end_to_end/test_multiple_whitelists/pixi.toml")
131-
)] check_config: CondaDenyCheckConfig,
122+
Some(PathBuf::from("tests/test_end_to_end/test_multiple_whitelists/pixi.toml")),
123+
Some(vec!["tests/test_end_to_end/test_multiple_whitelists/pixi.lock".into()])
124+
)]
125+
check_config: CondaDenyCheckConfig,
132126
mut out: Vec<u8>,
133127
) {
134128
let result = check(check_config, &mut out);
@@ -137,9 +131,8 @@ fn test_multiple_whitelists_check(
137131

138132
#[rstest]
139133
fn test_multiple_whitelists_list(
140-
#[with(
141-
PathBuf::from("tests/test_end_to_end/test_multiple_whitelists/pixi.toml")
142-
)] list_config: CondaDenyListConfig,
134+
#[with(Some(PathBuf::from("tests/test_end_to_end/test_multiple_whitelists/pixi.toml")))]
135+
list_config: CondaDenyListConfig,
143136
mut out: Vec<u8>,
144137
) {
145138
// todo this test doesn't make sense
@@ -149,7 +142,10 @@ fn test_multiple_whitelists_list(
149142

150143
#[rstest]
151144
fn test_config_with_platform_and_env(
152-
#[with(PathBuf::from("tests/test_end_to_end/test_platform_env_spec/pixi.toml"))]
145+
#[with(
146+
Some(PathBuf::from("tests/test_end_to_end/test_platform_env_spec/pixi.toml")),
147+
Some(vec!["tests/test_end_to_end/test_platform_env_spec/pixi.lock".into()])
148+
)]
153149
check_config: CondaDenyCheckConfig,
154150
mut out: Vec<u8>,
155151
) {
@@ -160,8 +156,8 @@ fn test_config_with_platform_and_env(
160156
#[rstest]
161157
fn test_osi_check(
162158
#[with(
163-
PathBuf::from("tests/test_end_to_end/test_osi_check/pixi.toml"),
164159
None,
160+
Some(vec!["tests/test_end_to_end/test_osi_check/pixi.toml".into()]),
165161
None,
166162
None,
167163
None,
@@ -180,7 +176,7 @@ fn test_osi_check(
180176
#[rstest]
181177
fn test_prefix_list(
182178
#[with(
183-
PathBuf::from("tests/test_end_to_end/test_prefix_list/pixi.toml"), None, Some(vec!["../../../tests/test_conda_prefixes/test-env".into()])
179+
Some(PathBuf::from("tests/test_end_to_end/test_prefix_list/pixi.toml")), None, Some(vec!["../../../tests/test_conda_prefixes/test-env".into()])
184180
)]
185181
list_config: CondaDenyListConfig,
186182
mut out: Vec<u8>,

0 commit comments

Comments
 (0)