package acceptance;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.nio.file.Files;
import java.nio.file.Path;

import static acceptance.TestUtils.copyResource;
import static acceptance.TestUtils.main;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class ConfigIT
{

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    private Path root()
    {
        return folder.getRoot().toPath().toAbsolutePath();
    }

    @Test
    public void propertyByFile() throws Exception
    {
        copyResource("acceptance/params.yml", root().resolve("params.yml"));
        TestUtils.fakeHome(root().resolve("home").toString(), () -> {
            Files.write(root().resolve("home").resolve(".digdag").resolve("config"), "params.mysql.password=secret".getBytes(UTF_8));
            main("run", "-o", root().toString(), "-f", root().resolve("params.yml").toString());
        });
        assertThat(Files.readAllBytes(root().resolve("foo.out")), is("secret\n".getBytes(UTF_8)));
    }
}

@@ -302,16 +302,62 @@
 .UTF_8;%0A
+import static org.hamcrest.Matchers.contains;%0A
 import s
@@ -388,16 +388,16 @@
 ers.is;%0A
-
 import s
@@ -665,16 +665,28 @@
 ByFile()
+%0A           
  throws 
@@ -1091,16 +1091,16 @@
     %7D);%0A
-
         
@@ -1195,11 +1195,706 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void verifyThatCommandLineParamsOverridesConfigFileParams()%0A            throws Exception%0A    %7B%0A        copyResource(%22acceptance/params.yml%22, root().resolve(%22params.yml%22));%0A        TestUtils.fakeHome(root().resolve(%22home%22).toString(), () -%3E %7B%0A            Files.write(root().resolve(%22home%22).resolve(%22.digdag%22).resolve(%22config%22), %22params.mysql.password=secret%22.getBytes(UTF_8));%0A            main(%22run%22,%0A                    %22-o%22, root().toString(),%0A                    %22-f%22, root().resolve(%22params.yml%22).toString(),%0A                    %22-p%22, %22mysql.password=override%22);%0A        %7D);%0A        assertThat(Files.readAllLines(root().resolve(%22foo.out%22)), contains(%22override%22));%0A    %7D
 %0A%7D%0A

verify command line param overrides config param