package org.jenkinsci.plugins.workflow.steps;

import jenkins.util.VirtualFile;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.util.Arrays;

/**
 * @author Kohsuke Kawaguchi
 */
public class ArtifactArchiverStepTest extends Assert {
    @Rule
    public JenkinsRule j = new JenkinsRule();

    /**
     * Archive and unarchive file
     */
    @Test
    public void archive() throws Exception {
        // job setup
        WorkflowJob foo = j.jenkins.createProject(WorkflowJob.class, "foo");
        foo.setDefinition(new CpsFlowDefinition(StringUtils.join(Arrays.asList(
                "node {",
                "  sh 'echo hello world > msg'",
                "  archive 'm*'",
                "  unarchive(mapping:['msg':'msg.out'])",
                "  archive 'msg.out'",
                "}"), "\n")));


        // get the build going, and wait until workflow pauses
        WorkflowRun b = j.assertBuildStatusSuccess(foo.scheduleBuild2(0).get());

        VirtualFile archivedFile = b.getArtifactManager().root().child("msg.out");
        assertTrue(archivedFile.exists());
        assertEquals("hello world\n",IOUtils.toString(archivedFile.open()));
    }

    @Test public void unarchiveDir() throws Exception {
        WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
        p.setDefinition(new CpsFlowDefinition(StringUtils.join(Arrays.asList(
                "node {",
                "  sh 'mkdir -p a/b && echo one > a/1 && echo two > a/b/2'",
                "  archive 'a/'",
                "  sh 'rm -r a'",
                "  unarchive mapping: ['a/' : '.']",
                "  sh 'cat a/1 a/b/2'",
                "}"), "\n")));
        WorkflowRun b = j.assertBuildStatusSuccess(p.scheduleBuild2(0).get());
        VirtualFile archivedFile = b.getArtifactManager().root().child("a/b/2");
        assertTrue(archivedFile.exists());
        assertEquals("two\n", IOUtils.toString(archivedFile.open()));
        j.assertLogContains("one\ntwo", b);
    }

}


@@ -947,17 +947,25 @@
  %22  
-sh 'echo 
+writeFile text: '
 hell
@@ -971,19 +971,26 @@
 lo world
- %3E 
+', file: '
 msg'%22,%0A 
@@ -1456,20 +1456,19 @@
 lo world
-%5Cn
 %22,
+ 
 IOUtils.
@@ -1762,57 +1762,74 @@
  %22  
-sh 'mkdir -p a/b && echo one %3E a/1 && echo two %3E 
+writeFile text: 'one', file: 'a/1'; writeFile text: 'two', file: '
 a/b/
@@ -1890,20 +1890,20 @@
  %22  
-sh 'rm -r a'
+dir('new') %7B
 %22,%0A 
@@ -1910,32 +1910,34 @@
                %22
+  
   unarchive mapp
@@ -1979,26 +1979,79 @@
  %22  
-sh 'cat a/1 a/b/2'
+  echo %5C%22$%7BreadFile 'a/1'%7D/$%7BreadFile 'a/b/2'%7D%5C%22%22,%0A                %22  %7D
 %22,%0A 
@@ -2308,18 +2308,16 @@
 als(%22two
-%5Cn
 %22, IOUti
@@ -2383,18 +2383,17 @@
 ins(%22one
-%5Cn
+/
 two%22, b)

Make this pass also on Windows.