Skip to content
Snippets Groups Projects
Commit 74c8a370 authored by Yaroslav Dynnikov's avatar Yaroslav Dynnikov
Browse files

Fix env vars precedence

The clap module we use for parsing command-line arguments allows
specifying `default_value`. Unfortunately, it makes impossible to tell
if an argument was actually passed. So it always overrides env var with
a default value, which is undesirable.
parent 618432e6
No related branches found
No related tags found
1 merge request!11Fix env vars precedence
Pipeline #3398 passed
...@@ -5,19 +5,23 @@ subcommands: ...@@ -5,19 +5,23 @@ subcommands:
- run: - run:
about: run the picodata instance about: run the picodata instance
args: args:
# Don't use `default_value` here,
# it breaks env vars precedence
- data-dir: - data-dir:
long: data-dir long: data-dir
help: Here instance persists all its data help: >
Here instance persists all its data
(default: .)
takes_value: true takes_value: true
default_value: "."
value_name: path value_name: path
- listen: - listen:
# short: l # short: l
long: listen long: listen
help: Socket bind address help: >
Socket bind address (default: 3301)
takes_value: true takes_value: true
default_value: "0.0.0.0:3301"
value_name: "[host:]port" value_name: "[host:]port"
- peer: - peer:
......
...@@ -65,6 +65,10 @@ fn main_run(matches: &clap::ArgMatches) { ...@@ -65,6 +65,10 @@ fn main_run(matches: &clap::ArgMatches) {
} }
envp.insert("PICODATA_COMMAND".to_owned(), "run".to_owned()); envp.insert("PICODATA_COMMAND".to_owned(), "run".to_owned());
envp.entry("PICODATA_LISTEN".to_owned())
.or_insert("3301".to_owned());
envp.entry("PICODATA_DATA_DIR".to_owned())
.or_insert(".".to_owned());
let bypass_vars = [ let bypass_vars = [
"cluster-id", "cluster-id",
......
...@@ -11,7 +11,18 @@ fn positive() { ...@@ -11,7 +11,18 @@ fn positive() {
let mut cmd = Command::cargo_bin("picodata").unwrap(); let mut cmd = Command::cargo_bin("picodata").unwrap();
cmd.current_dir(temp_path); cmd.current_dir(temp_path);
cmd.arg("run"); cmd.arg("run");
cmd.args(["-e", "os.exit()"]); cmd.arg("-e").arg(
r#"
function assert_eq(l, r)
if l ~= r then
error(('Assertion failed: %q ~= %q'):format(l, r), 2)
end
end
assert_eq(os.environ()['PICODATA_LISTEN'], "3301")
assert_eq(os.environ()['PICODATA_DATA_DIR'], ".")
os.exit()
"#,
);
cmd.timeout(Duration::from_secs(1)).assert().success(); cmd.timeout(Duration::from_secs(1)).assert().success();
} }
...@@ -104,3 +115,28 @@ fn pass_environment() { ...@@ -104,3 +115,28 @@ fn pass_environment() {
"tarantool didn't make a snapshot" "tarantool didn't make a snapshot"
); );
} }
#[test]
fn precedence() {
let temp = tempdir().unwrap();
let temp_path = temp.path();
let mut cmd = Command::cargo_bin("picodata").unwrap();
cmd.current_dir(temp_path);
cmd.arg("run");
cmd.env("PICODATA_DATA_DIR", "./somewhere");
cmd.env("PICODATA_LISTEN", "0.0.0.0:0");
cmd.arg("-e").arg(
r#"
function assert_eq(l, r)
if l ~= r then
error(('Assertion failed: %q ~= %q'):format(l, r), 2)
end
end
assert_eq(os.environ()['PICODATA_DATA_DIR'], './somewhere')
assert_eq(os.environ()['PICODATA_LISTEN'], "0.0.0.0:0")
os.exit(0)
"#,
);
cmd.timeout(Duration::from_secs(1)).assert().success();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment