diff --git a/build.gradle b/build.gradle index 5e1ab97..903624f 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ dependencies { provided "org.embulk:embulk-core:0.7.0" // compile "YOUR_JAR_DEPENDENCY_GROUP:YOUR_JAR_DEPENDENCY_MODULE:YOUR_JAR_DEPENDENCY_VERSION" testCompile "junit:junit:4.+" + testCompile 'org.embulk:embulk-core:0.7.+:tests' } task classpath(type: Copy, dependsOn: ["jar"]) { diff --git a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java index 5a2e76f..691a24b 100644 --- a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java +++ b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java @@ -5,6 +5,8 @@ import java.io.OutputStream; import java.io.FilterOutputStream; import java.io.IOException; + +import com.google.common.annotations.VisibleForTesting; import org.slf4j.Logger; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableList; @@ -32,11 +34,6 @@ public interface PluginTask public String getCommand(); } - public static final List SHELL = ImmutableList.of( - // TODO use ["PowerShell.exe", "-Command"] on windows? - "sh", "-c" - ); - @Override public ConfigDiff transaction(ConfigSource config, int taskCount, FileOutputPlugin.Control control) @@ -68,8 +65,9 @@ public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) { PluginTask task = taskSource.loadTask(PluginTask.class); + List shell = new ShellFactory().build().get(); List cmdline = new ArrayList(); - cmdline.addAll(SHELL); + cmdline.addAll(shell); cmdline.add(task.getCommand()); logger.info("Using command {}", cmdline); @@ -77,6 +75,26 @@ public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) return new PluginFileOutput(cmdline, taskIndex); } + @VisibleForTesting + static class ShellFactory + { + private List shell; + + public List get() { + return this.shell; + } + + public ShellFactory build() { + String osName = System.getProperty("os.name"); + if(osName.indexOf("Windows") >= 0) { + this.shell = ImmutableList.of("PowerShell.exe", "-Command"); + } else { + this.shell = ImmutableList.of("sh", "-c"); + } + return this; + } + } + private static class ProcessWaitOutputStream extends FilterOutputStream { diff --git a/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java b/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java index 22d1032..52ff848 100644 --- a/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java +++ b/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java @@ -1,5 +1,39 @@ package org.embulk.output; +import com.google.common.collect.ImmutableList; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.embulk.EmbulkTestRuntime; +import org.embulk.output.CommandFileOutputPlugin.ShellFactory; + +import java.util.List; + +import static org.junit.Assert.assertEquals; + public class TestCommandFileOutputPlugin { + @Rule + public EmbulkTestRuntime runtime = new EmbulkTestRuntime(); + + private ShellFactory shellFactory; + + @Before + public void createResources() + { + shellFactory = new ShellFactory().build(); + } + + @Test + public void testShell() { + List shell = shellFactory.get(); + String osName = System.getProperty("os.name"); + List actualShellCmd; + if (osName.indexOf("Windows") >= 0) { + actualShellCmd = ImmutableList.of("PowerShell.exe", "-Command"); + } else { + actualShellCmd = ImmutableList.of("sh", "-c"); + } + assertEquals(actualShellCmd, shell); + } }