summaryrefslogtreecommitdiff
path: root/src/build.rs
blob: b56e68021b13b1bc1c2810fb89e054ef8d1298fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
use anyhow::{Context as _, Result};
use log::{debug, info};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::{Duration, Instant};
use tokio::io::{AsyncWrite, AsyncWriteExt as _, BufWriter};
use tokio::process::Command;

use crate::repo_config::RepoConfig;

/// Optional container resource limits and network mode.
///
/// Passed from `SiteConfig` to `execute()` to inject `--memory`, `--cpus`,
/// `--pids-limit`, and `--network` flags into the container command.
#[derive(Debug)]
pub struct ContainerOptions {
    pub memory: Option<String>,
    pub cpus: Option<f64>,
    pub pids_limit: Option<u32>,
    pub network: String,
    pub workdir: Option<String>,
}

impl Default for ContainerOptions {
    fn default() -> Self {
        Self {
            memory: None,
            cpus: None,
            pids_limit: None,
            network: "bridge".to_owned(),
            workdir: None,
        }
    }
}

/// Default timeout for build operations.
pub const BUILD_TIMEOUT_DEFAULT: Duration = Duration::from_secs(600); // 10 minutes

/// Size of the in-memory tail buffer for stderr (last 1 KB).
/// Used for `BuildFailure::Display` after streaming to disk.
const STDERR_TAIL_SIZE: usize = 1024;

/// Result of a build execution.
///
/// Stdout and stderr are streamed to temporary files on disk during the build.
/// Callers should pass these paths to `logs::save_build_log()` for composition.
#[derive(Debug)]
pub struct BuildResult {
    pub stdout_file: PathBuf,
    pub stderr_file: PathBuf,
    pub duration: Duration,
}

/// Error from a failed build command.
///
/// Carries structured exit code and file paths to captured output.
/// `last_stderr` holds the last 1 KB of stderr for the `Display` impl.
#[derive(Debug)]
pub struct BuildFailure {
    pub exit_code: i32,
    pub stdout_file: PathBuf,
    pub stderr_file: PathBuf,
    pub last_stderr: String,
    pub duration: Duration,
}

impl std::fmt::Display for BuildFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "build failed with exit code {}: {}",
            self.exit_code,
            self.last_stderr.trim()
        )
    }
}

impl std::error::Error for BuildFailure {}

/// Writer that duplicates all writes to both a primary and secondary writer.
///
/// Used for `--verbose` mode: streams build output to both a temp file (primary)
/// and stderr (secondary) simultaneously.
pub struct TeeWriter<W> {
    primary: W,
    secondary: tokio::io::Stderr,
}

impl<W: AsyncWrite + Unpin> TeeWriter<W> {
    pub const fn new(primary: W, secondary: tokio::io::Stderr) -> Self {
        Self { primary, secondary }
    }
}

impl<W: AsyncWrite + Unpin> AsyncWrite for TeeWriter<W> {
    fn poll_write(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<std::io::Result<usize>> {
        // Write to primary first
        let poll = std::pin::Pin::new(&mut self.primary).poll_write(cx, buf);
        if let std::task::Poll::Ready(Ok(n)) = &poll {
            // Best-effort write to secondary (stderr) — same bytes
            let _ = std::pin::Pin::new(&mut self.secondary).poll_write(cx, &buf[..*n]);
        }
        poll
    }

    fn poll_flush(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        let _ = std::pin::Pin::new(&mut self.secondary).poll_flush(cx);
        std::pin::Pin::new(&mut self.primary).poll_flush(cx)
    }

    fn poll_shutdown(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<()>> {
        let _ = std::pin::Pin::new(&mut self.secondary).poll_shutdown(cx);
        std::pin::Pin::new(&mut self.primary).poll_shutdown(cx)
    }
}

/// Build the container CLI arguments for a build invocation.
///
/// Assembles the full `run --rm ...` argument list including volume mounts,
/// environment variables, resource limits, and the build command.
fn build_container_args(
    runtime: &str,
    clone_dir: &Path,
    repo_config: &RepoConfig,
    cache_volumes: &[(String, PathBuf)],
    env: &HashMap<String, String>,
    options: &ContainerOptions,
) -> Vec<String> {
    let mut args = vec![
        "run".to_owned(),
        "--rm".to_owned(),
        "--volume".to_owned(),
        format!("{}:/workspace:Z", clone_dir.display()),
    ];

    // Cache volume mounts
    for (container_path, host_path) in cache_volumes {
        args.push("--volume".to_owned());
        args.push(format!("{}:{}:Z", host_path.display(), container_path));
    }

    // User-defined environment variables
    for (key, value) in env {
        args.push("--env".to_owned());
        args.push(format!("{key}={value}"));
    }

    let workdir = match &options.workdir {
        Some(subdir) => format!("/workspace/{subdir}"),
        None => "/workspace".to_owned(),
    };
    args.extend(["--workdir".to_owned(), workdir, "--cap-drop=ALL".to_owned()]);

    if runtime == "podman" {
        args.push("--userns=keep-id".to_owned());
    } else {
        args.push("--cap-add=DAC_OVERRIDE".to_owned());
    }

    // Resource limits
    if let Some(memory) = &options.memory {
        args.push("--memory".to_owned());
        args.push(memory.clone());
    }
    if let Some(cpus) = options.cpus {
        args.push("--cpus".to_owned());
        args.push(cpus.to_string());
    }
    if let Some(pids) = options.pids_limit {
        args.push("--pids-limit".to_owned());
        args.push(pids.to_string());
    }

    // Network mode
    args.push(format!("--network={}", options.network));

    args.extend([
        repo_config.image.clone(),
        "sh".to_owned(),
        "-c".to_owned(),
        repo_config.command.clone(),
    ]);

    args
}

/// Execute a containerized build for a site.
///
/// Stdout and stderr are streamed to the provided temporary files on disk
/// instead of being buffered in memory. This removes unbounded memory usage
/// for container builds.
///
/// # Arguments
/// * `runtime` - Container runtime to use ("podman" or "docker")
/// * `clone_dir` - Path to the cloned repository
/// * `repo_config` - Build configuration from witryna.yaml
/// * `cache_volumes` - Pairs of (`container_path`, `host_path`) for persistent cache mounts
/// * `env` - User-defined environment variables to pass into the container via `--env`
/// * `options` - Optional container resource limits and network mode
/// * `stdout_file` - Temp file path for captured stdout
/// * `stderr_file` - Temp file path for captured stderr
/// * `timeout` - Maximum duration before killing the build
/// * `verbose` - When true, also stream build output to stderr in real-time
///
/// # Errors
///
/// Returns an error if the container command times out, fails to execute,
/// or exits with a non-zero status code (as a [`BuildFailure`]).
///
/// # Security
/// - Uses typed arguments (no shell interpolation) per OWASP guidelines
/// - Mounts clone directory as read-write (needed for build output)
/// - Runs with minimal capabilities
#[allow(clippy::implicit_hasher, clippy::too_many_arguments)]
pub async fn execute(
    runtime: &str,
    clone_dir: &Path,
    repo_config: &RepoConfig,
    cache_volumes: &[(String, PathBuf)],
    env: &HashMap<String, String>,
    options: &ContainerOptions,
    stdout_file: &Path,
    stderr_file: &Path,
    timeout: Duration,
    verbose: bool,
) -> Result<BuildResult> {
    info!(
        "executing container build: image={} command={} path={}",
        repo_config.image,
        repo_config.command,
        clone_dir.display()
    );

    let start = Instant::now();
    let args = build_container_args(runtime, clone_dir, repo_config, cache_volumes, env, options);

    // Spawn with piped stdout/stderr for streaming (OWASP: no shell interpolation)
    let mut child = Command::new(runtime)
        .args(&args)
        .kill_on_drop(true)
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .context("failed to spawn container build")?;

    let stdout_pipe = child
        .stdout
        .take()
        .ok_or_else(|| anyhow::anyhow!("missing stdout pipe"))?;
    let stderr_pipe = child
        .stderr
        .take()
        .ok_or_else(|| anyhow::anyhow!("missing stderr pipe"))?;

    let stdout_file_writer = BufWriter::new(
        tokio::fs::File::create(stdout_file)
            .await
            .with_context(|| format!("failed to create {}", stdout_file.display()))?,
    );
    let stderr_file_writer = BufWriter::new(
        tokio::fs::File::create(stderr_file)
            .await
            .with_context(|| format!("failed to create {}", stderr_file.display()))?,
    );

    if verbose {
        let mut stdout_tee = TeeWriter::new(stdout_file_writer, tokio::io::stderr());
        let mut stderr_tee = TeeWriter::new(stderr_file_writer, tokio::io::stderr());
        Box::pin(run_build_process(
            child,
            stdout_pipe,
            stderr_pipe,
            &mut stdout_tee,
            &mut stderr_tee,
            start,
            stdout_file,
            stderr_file,
            clone_dir,
            "container",
            timeout,
        ))
        .await
    } else {
        let mut stdout_writer = stdout_file_writer;
        let mut stderr_writer = stderr_file_writer;
        Box::pin(run_build_process(
            child,
            stdout_pipe,
            stderr_pipe,
            &mut stdout_writer,
            &mut stderr_writer,
            start,
            stdout_file,
            stderr_file,
            clone_dir,
            "container",
            timeout,
        ))
        .await
    }
}

/// Copy from reader to writer, keeping the last `tail_size` bytes in memory.
/// Returns `(total_bytes_copied, tail_buffer)`.
///
/// When `tail_size` is 0, skips tail tracking entirely (used for stdout
/// where we don't need a tail). The tail buffer is used to provide a
/// meaningful error message in `BuildFailure::Display` without reading
/// the entire stderr file back into memory.
#[allow(clippy::indexing_slicing)] // buf[..n] bounded by read() return value
pub async fn copy_with_tail<R, W>(
    mut reader: R,
    mut writer: W,
    tail_size: usize,
) -> std::io::Result<(u64, Vec<u8>)>
where
    R: tokio::io::AsyncRead + Unpin,
    W: tokio::io::AsyncWrite + Unpin,
{
    use tokio::io::AsyncReadExt as _;

    let mut buf = [0_u8; 8192];
    let mut total: u64 = 0;
    let mut tail: Vec<u8> = Vec::new();

    loop {
        let n = reader.read(&mut buf).await?;
        if n == 0 {
            break;
        }
        writer.write_all(&buf[..n]).await?;
        total += n as u64;

        if tail_size > 0 {
            tail.extend_from_slice(&buf[..n]);
            if tail.len() > tail_size {
                let excess = tail.len() - tail_size;
                tail.drain(..excess);
            }
        }
    }

    Ok((total, tail))
}

/// Shared build-process loop: stream stdout/stderr through writers, handle timeout and exit status.
#[allow(clippy::too_many_arguments)]
async fn run_build_process<W1, W2>(
    mut child: tokio::process::Child,
    stdout_pipe: tokio::process::ChildStdout,
    stderr_pipe: tokio::process::ChildStderr,
    stdout_writer: &mut W1,
    stderr_writer: &mut W2,
    start: Instant,
    stdout_file: &Path,
    stderr_file: &Path,
    clone_dir: &Path,
    label: &str,
    timeout: Duration,
) -> Result<BuildResult>
where
    W1: AsyncWrite + Unpin,
    W2: AsyncWrite + Unpin,
{
    #[allow(clippy::large_futures)]
    let Ok((stdout_res, stderr_res, wait_res)) = tokio::time::timeout(timeout, async {
        let (stdout_res, stderr_res, wait_res) = tokio::join!(
            copy_with_tail(stdout_pipe, &mut *stdout_writer, 0),
            copy_with_tail(stderr_pipe, &mut *stderr_writer, STDERR_TAIL_SIZE),
            child.wait(),
        );
        (stdout_res, stderr_res, wait_res)
    })
    .await
    else {
        let _ = child.kill().await;
        anyhow::bail!("{label} build timed out after {}s", timeout.as_secs());
    };

    stdout_res.context("failed to stream stdout")?;
    let (_, stderr_tail) = stderr_res.context("failed to stream stderr")?;
    stdout_writer.flush().await?;
    stderr_writer.flush().await?;

    let status = wait_res.context(format!("{label} build I/O error"))?;
    let last_stderr = String::from_utf8_lossy(&stderr_tail).into_owned();

    if !status.success() {
        let exit_code = status.code().unwrap_or(-1);
        debug!("{label} build failed: exit_code={exit_code}");
        return Err(BuildFailure {
            exit_code,
            stdout_file: stdout_file.to_path_buf(),
            stderr_file: stderr_file.to_path_buf(),
            last_stderr,
            duration: start.elapsed(),
        }
        .into());
    }

    let duration = start.elapsed();
    debug!(
        "{label} build completed: path={} duration={duration:?}",
        clone_dir.display()
    );
    Ok(BuildResult {
        stdout_file: stdout_file.to_path_buf(),
        stderr_file: stderr_file.to_path_buf(),
        duration,
    })
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::indexing_slicing,
    clippy::large_futures,
    clippy::print_stderr
)]
mod tests {
    use super::*;
    use crate::test_support::{cleanup, temp_dir};
    use tokio::fs;
    use tokio::process::Command as TokioCommand;

    /// Check if a container runtime is available and its daemon is running.
    async fn container_runtime_available(runtime: &str) -> bool {
        TokioCommand::new(runtime)
            .args(["info"])
            .stdout(Stdio::null())
            .stderr(Stdio::null())
            .status()
            .await
            .map(|s| s.success())
            .unwrap_or(false)
    }

    /// Get the first available container runtime.
    async fn get_runtime() -> Option<String> {
        for runtime in &["podman", "docker"] {
            if container_runtime_available(runtime).await {
                return Some((*runtime).to_owned());
            }
        }
        None
    }

    // --- copy_with_tail() unit tests ---

    #[tokio::test]
    async fn copy_with_tail_small_input() {
        let input = b"hello";
        let mut output = Vec::new();
        let (total, tail) = copy_with_tail(&input[..], &mut output, 1024).await.unwrap();
        assert_eq!(total, 5);
        assert_eq!(tail, b"hello");
        assert_eq!(output, b"hello");
    }

    #[tokio::test]
    async fn copy_with_tail_large_input() {
        // Input larger than tail_size — only last N bytes kept
        let input: Vec<u8> = (0_u8..=255).cycle().take(2048).collect();
        let mut output = Vec::new();
        let (total, tail) = copy_with_tail(&input[..], &mut output, 512).await.unwrap();
        assert_eq!(total, 2048);
        assert_eq!(tail.len(), 512);
        assert_eq!(&tail[..], &input[2048 - 512..]);
        assert_eq!(output, input);
    }

    #[tokio::test]
    async fn copy_with_tail_zero_tail() {
        let input = b"data";
        let mut output = Vec::new();
        let (total, tail) = copy_with_tail(&input[..], &mut output, 0).await.unwrap();
        assert_eq!(total, 4);
        assert!(tail.is_empty());
        assert_eq!(output, b"data");
    }

    // --- ContainerOptions workdir tests ---

    #[tokio::test]
    async fn execute_custom_workdir_runs_from_subdir() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-workdir-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        // Create a subdirectory with a marker file
        let subdir = temp.join("packages").join("frontend");
        fs::create_dir_all(&subdir).await.unwrap();
        fs::write(subdir.join("marker.txt"), "subdir-marker")
            .await
            .unwrap();

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "cat marker.txt".to_owned(),
            public: "dist".to_owned(),
        };

        let options = ContainerOptions {
            workdir: Some("packages/frontend".to_owned()),
            ..ContainerOptions::default()
        };

        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &options,
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_ok(), "build should succeed: {result:?}");
        let stdout = fs::read_to_string(&stdout_tmp).await.unwrap();
        assert!(
            stdout.contains("subdir-marker"),
            "should read marker from subdir, got: {stdout}"
        );

        cleanup(&temp).await;
    }

    // --- execute() container tests (Tier 2) ---

    #[tokio::test]
    async fn execute_simple_command_success() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "echo 'hello world'".to_owned(),
            public: "dist".to_owned(),
        };

        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_ok(), "build should succeed: {result:?}");
        let stdout = fs::read_to_string(&stdout_tmp).await.unwrap();
        assert!(stdout.contains("hello world"));

        cleanup(&temp).await;
    }

    #[tokio::test]
    async fn execute_creates_output_files() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "mkdir -p dist && echo 'content' > dist/index.html".to_owned(),
            public: "dist".to_owned(),
        };

        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_ok(), "build should succeed: {result:?}");

        // Verify output file was created
        let output_file = temp.join("dist/index.html");
        assert!(output_file.exists(), "output file should exist");

        let content = fs::read_to_string(&output_file).await.unwrap();
        assert!(content.contains("content"));

        cleanup(&temp).await;
    }

    #[tokio::test]
    async fn execute_failing_command_returns_error() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "exit 1".to_owned(),
            public: "dist".to_owned(),
        };

        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_err(), "build should fail");
        let err = result.unwrap_err().to_string();
        assert!(err.contains("exit code 1"));

        cleanup(&temp).await;
    }

    #[tokio::test]
    async fn execute_command_with_stderr() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "echo 'error message' >&2 && exit 1".to_owned(),
            public: "dist".to_owned(),
        };

        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_err(), "build should fail");
        let err = result.unwrap_err().to_string();
        assert!(err.contains("error message"));

        cleanup(&temp).await;
    }

    #[tokio::test]
    async fn execute_invalid_image_returns_error() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        let repo_config = RepoConfig {
            image: "nonexistent-image-xyz-12345:latest".to_owned(),
            command: "echo hello".to_owned(),
            public: "dist".to_owned(),
        };

        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_err(), "build should fail for invalid image");

        cleanup(&temp).await;
    }

    #[tokio::test]
    async fn execute_workdir_is_correct() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        // Create a file in the temp dir to verify we can see it
        fs::write(temp.join("marker.txt"), "test-marker")
            .await
            .unwrap();

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "cat marker.txt".to_owned(),
            public: "dist".to_owned(),
        };

        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_ok(), "build should succeed: {result:?}");
        let stdout = fs::read_to_string(&stdout_tmp).await.unwrap();
        assert!(stdout.contains("test-marker"));

        cleanup(&temp).await;
    }

    #[tokio::test]
    async fn execute_invalid_runtime_returns_error() {
        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "echo hello".to_owned(),
            public: "dist".to_owned(),
        };

        let result = execute(
            "nonexistent-runtime-xyz",
            &temp,
            &repo_config,
            &[],
            &HashMap::new(),
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_err(), "build should fail for invalid runtime");

        cleanup(&temp).await;
    }

    #[tokio::test]
    async fn execute_with_env_vars_passes_to_container() {
        let Some(runtime) = get_runtime().await else {
            eprintln!("Skipping test: no container runtime available");
            return;
        };

        let temp = temp_dir("build-test").await;
        let stdout_tmp = temp.join("stdout.tmp");
        let stderr_tmp = temp.join("stderr.tmp");

        let repo_config = RepoConfig {
            image: "alpine:latest".to_owned(),
            command: "printenv MY_VAR".to_owned(),
            public: "dist".to_owned(),
        };

        let env = HashMap::from([("MY_VAR".to_owned(), "my_value".to_owned())]);
        let result = execute(
            &runtime,
            &temp,
            &repo_config,
            &[],
            &env,
            &ContainerOptions::default(),
            &stdout_tmp,
            &stderr_tmp,
            BUILD_TIMEOUT_DEFAULT,
            false,
        )
        .await;

        assert!(result.is_ok(), "build should succeed: {result:?}");
        let stdout = fs::read_to_string(&stdout_tmp).await.unwrap();
        assert!(
            stdout.contains("my_value"),
            "stdout should contain env var value, got: {stdout}",
        );

        cleanup(&temp).await;
    }
}