Skip to content
Open

fix #20

Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target
temp/test*
file.txt
file.txt
version.txt
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ARG VERSION=1.0.0
ENV APP_VERSION=$VERSION
ENV BUILD_TYPE=release
RUN echo "Building version $APP_VERSION in $BUILD_TYPE mode"
RUN echo "VERSION=$VERSION" > version.txt
RUN echo "APP_VERSION=$APP_VERSION" >> version.txt
RUN echo "BUILD_TYPE=$BUILD_TYPE" >> version.txt
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::{Arg, Command};
use colored::*;
use regex::Regex;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::{self, BufRead, Write};
Expand Down Expand Up @@ -77,7 +76,6 @@ fn main() {
let arg_re = Regex::new(r"^ARG\s+([^=\s]+)(?:\s*=\s*(.+))?").unwrap();
let workdir_re = Regex::new(r"^WORKDIR\s+(.+)").unwrap();

let mut args_map: HashMap<String, String> = HashMap::new();
let mut run_command = String::new();
let mut in_run_block = false;
let workdir = env::current_dir().unwrap();
Expand Down Expand Up @@ -258,7 +256,7 @@ fn main() {
format!("Action: Setting ARG variable: {}={}", key, value).magenta()
);
}
args_map.insert(key, value);
env::set_var(key, value);
} else {
let env_value = env::var(&key).ok();
if debug_enabled {
Expand Down Expand Up @@ -327,7 +325,7 @@ fn main() {
format!("Action: Setting ARG variable: {}={}", key, value).magenta()
);
}
args_map.insert(key, value);
env::set_var(key, value);
};
} else if !line.is_empty() && !line.starts_with('#') && debug_enabled {
println!(
Expand Down
63 changes: 63 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,67 @@ RUN pwd"#;

cleanup_test_dir(test_dir);
}

#[test]
fn test_arg_env_interaction() {
let dockerfile_content = r#"ARG VERSION=1.0.0
ENV APP_VERSION=$VERSION
ENV BUILD_TYPE=release
RUN echo "Building version $APP_VERSION in $BUILD_TYPE mode"
RUN echo "VERSION=$VERSION" > version.txt
RUN echo "APP_VERSION=$APP_VERSION" >> version.txt
RUN echo "BUILD_TYPE=$BUILD_TYPE" >> version.txt"#;

let (test_dir, dockerfile_path) = create_test_dockerfile(dockerfile_content, "arg_env");
println!("Dockerfile path: {:?}", dockerfile_path);

let mut child = Command::new("cargo")
.args(["run", "--", "-f", dockerfile_path.to_str().unwrap()])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("Failed to spawn command");

// Send empty input to use default value for ARG
if let Some(mut stdin) = child.stdin.take() {
stdin.write_all(b"\n").expect("Failed to write to stdin");
}

let output = child.wait_with_output().expect("Failed to wait on child");

let stdout = String::from_utf8_lossy(&output.stdout);
println!("Command output: {}", stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
println!("Command stderr: {}", stderr);

assert!(output.status.success());

// Verify the version.txt file was created and contains the correct values
let version_file = PathBuf::from("version.txt");
println!("Checking version file path: {:?}", version_file);
assert!(version_file.exists(), "version.txt should exist");

let version_content =
fs::read_to_string(&version_file).expect("Failed to read version.txt");
println!("Version file content: {}", version_content);

let lines: Vec<&str> = version_content.lines().collect();
assert_eq!(lines.len(), 3, "version.txt should have 3 lines");

// Verify each line contains the expected value
assert!(
lines.iter().any(|line| *line == "VERSION=1.0.0"),
"version.txt should contain VERSION=1.0.0"
);
assert!(
lines.iter().any(|line| *line == "APP_VERSION=1.0.0"),
"version.txt should contain APP_VERSION=1.0.0"
);
assert!(
lines.iter().any(|line| *line == "BUILD_TYPE=release"),
"version.txt should contain BUILD_TYPE=release"
);

cleanup_test_dir(test_dir);
}
}
24 changes: 0 additions & 24 deletions test.Dockerfile

This file was deleted.

Loading