
expect_captured_length <- function(x, length) {
  expect_equal(length(capture.output(x)), length)
}

test_that("R6 creation works", {
  my_dock <- Dockerfile$new()
  expect_s3_class(my_dock, "R6")
  expect_s3_class(my_dock, "Dockerfile")
  my_dock$RUN("mkdir /usr/scripts")
  expect_captured_length(my_dock, 2)
  my_dock$ADD("from", "/usr/scripts")
  expect_captured_length(my_dock, 3)
  my_dock$COPY("from", "/usr/scripts")
  expect_captured_length(my_dock, 4)
  my_dock$WORKDIR("/usr/scripts")
  expect_captured_length(my_dock, 5)
  my_dock$EXPOSE(888)
  expect_captured_length(my_dock, 6)
  my_dock$VOLUME(" /usr/scripts")
  expect_captured_length(my_dock, 7)
  my_dock$CMD(" /usr/scripts")
  expect_captured_length(my_dock, 8)
  my_dock$LABEL("plop", "/usr/scripts")
  expect_captured_length(my_dock, 9)
  my_dock$ENV("plop", " /usr/scripts")
  expect_captured_length(my_dock, 10)
  my_dock$ENTRYPOINT("plop  /usr/scripts")
  expect_captured_length(my_dock, 11)
  my_dock$USER("plop  /usr/scripts")
  expect_captured_length(my_dock, 12)
  my_dock$ARG("plop /usr/scripts")
  expect_captured_length(my_dock, 13)
  my_dock$ONBUILD("plop /usr/scripts")
  expect_captured_length(my_dock, 14)
  my_dock$STOPSIGNAL("plop /usr/scripts")
  expect_captured_length(my_dock, 15)
  my_dock$HEALTHCHECK("plop /usr/scripts")
  expect_captured_length(my_dock, 16)
  my_dock$MAINTAINER("colin", "contact@colinfay.me")
  expect_captured_length(my_dock, 17)
  my_dock$SHELL("plop /usr/scripts")
  expect_captured_length(my_dock, 18)
  my_dock$remove_cmd(1)
  expect_captured_length(my_dock, 17)
  my_dock$switch_cmd(5, 6)
  expect_captured_length(my_dock, 17)
  my_dock$COMMENT("Just testing Dockerfile comments.")
  expect_captured_length(my_dock, 18)

  my_dock <- Dockerfile$new(FROM = "plop")
  expect_match(my_dock$Dockerfile, "plop")
  my_dock <- Dockerfile$new(FROM = "plop", AS = "pouet")
  expect_match(my_dock$Dockerfile, "plop")
  expect_match(my_dock$Dockerfile, "pouet")
})

test_that("warning works", {
  my_dock <- Dockerfile$new()
  # expect_warning(my_dock$COPY("nfi", "norifi", force = FALSE))
  expect_warning(my_dock$EXPOSE("9000"))
})

test_that("dock$ARG emits ARG <name> when no default is provided", {
  my_dock <- Dockerfile$new(FROM = "plop")
  my_dock$ARG("MYVAR")
  expect_true(any(grepl("^ARG MYVAR$", my_dock$Dockerfile)))
})

test_that("dock$ARG emits ARG <name>=<default> when a default is provided", {
  my_dock <- Dockerfile$new(FROM = "plop")
  my_dock$ARG("MYVAR", default = "myval")
  expect_true(any(grepl("^ARG MYVAR=myval$", my_dock$Dockerfile)))
})

test_that("dock$ARG default still respects ahead = TRUE", {
  my_dock <- Dockerfile$new(FROM = "plop")
  my_dock$RUN("placeholder")
  my_dock$ARG("MYVAR", ahead = TRUE, default = "myval")
  # FROM stays first; ARG is inserted before everything else (i.e. before
  # the FROM as well, because that's how `ahead = TRUE` is currently
  # implemented). We only assert the ARG line is present and well-formed.
  expect_true(any(my_dock$Dockerfile == "ARG MYVAR=myval"))
})

test_that("add_arg emits the right directive in both modes", {
  expect_equal(as.character(dockerfiler:::add_arg("MYVAR")), "ARG MYVAR")
  expect_equal(
    as.character(dockerfiler:::add_arg("MYVAR", default = "myval")),
    "ARG MYVAR=myval"
  )
})

test_that("add_arg errors when both `arg` contains `=` and `default` is supplied", {
  # Avoids producing the silently invalid `ARG MYVAR=oldval=newval`.
  expect_error(
    dockerfiler:::add_arg("MYVAR=oldval", default = "newval"),
    "already contains an `=` sign"
  )
  # Either form is fine on its own.
  expect_no_error(dockerfiler:::add_arg("MYVAR=oldval"))
  expect_no_error(dockerfiler:::add_arg("MYVAR", default = "newval"))
})

test_that("legacy inlined form `dock$ARG(\"NAME=value\")` keeps emitting `ARG NAME=value`", {
  # Backward-compat regression test: callers who pre-date the `default`
  # parameter must see the same output as before.
  my_dock <- Dockerfile$new(FROM = "plop")
  my_dock$ARG("MYVAR=myval")
  expect_true(any(my_dock$Dockerfile == "ARG MYVAR=myval"))
  expect_equal(
    as.character(dockerfiler:::add_arg("MYVAR=myval")),
    "ARG MYVAR=myval"
  )
})

test_that("dock$custom appends `<base> <cmd>` verbatim", {
  my_dock <- Dockerfile$new(FROM = "plop")
  my_dock$custom(base = "ONBUILD", cmd = "RUN echo hi")
  expect_true(any(my_dock$Dockerfile == "ONBUILD RUN echo hi"))
})

test_that("dock$add_after inserts a command at the requested position", {
  my_dock <- Dockerfile$new(FROM = "plop")
  my_dock$RUN("first")
  my_dock$RUN("third")
  my_dock$add_after(cmd = "RUN second", after = 2)
  expect_equal(my_dock$Dockerfile[3], "RUN second")
  expect_equal(my_dock$Dockerfile[2], "RUN first")
  expect_equal(my_dock$Dockerfile[4], "RUN third")
})
