From 33f1014459de77ebfca95e0cd978800a910779c7 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 10 Sep 2015 20:45:37 +0900 Subject: [PATCH 1/4] Add ShellFactory static class --- .../output/CommandFileOutputPlugin.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java index 5a2e76f..1255e60 100644 --- a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java +++ b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java @@ -77,6 +77,25 @@ public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) return new PluginFileOutput(cmdline, taskIndex); } + private static class ShellFactory + { + private List shell; + + public List get() { + return this.shell; + } + + public ShellFactory build() { + String osName = System.getProperty("os.name"); + if(osName.contains("Windows")) { + this.shell = ImmutableList.of("PowerShell.exe", "-Command"); + } else { + this.shell = ImmutableList.of("sh", "-c"); + } + return this; + } + } + private static class ProcessWaitOutputStream extends FilterOutputStream { From c368f7f2bace021c747498069e4265f2e8c9a7ce Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Thu, 10 Sep 2015 20:46:03 +0900 Subject: [PATCH 2/4] Use ShellFactory to support windows --- .../java/org/embulk/output/CommandFileOutputPlugin.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java index 1255e60..57a1b01 100644 --- a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java +++ b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java @@ -32,11 +32,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 +63,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); From 5e4342ea8f5d5b87ca06b87c8b1549b34c5159b9 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 11 Sep 2015 01:01:49 +0900 Subject: [PATCH 3/4] Add ShellFactory get shell test --- build.gradle | 1 + .../output/CommandFileOutputPlugin.java | 5 ++- .../output/TestCommandFileOutputPlugin.java | 34 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) 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 57a1b01..cd7a46e 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; @@ -73,7 +75,8 @@ public TransactionalFileOutput open(TaskSource taskSource, final int taskIndex) return new PluginFileOutput(cmdline, taskIndex); } - private static class ShellFactory + @VisibleForTesting + static class ShellFactory { private List shell; diff --git a/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java b/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java index 22d1032..7e38c2e 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.contains("Windows")) { + actualShellCmd = ImmutableList.of("PowerShell.exe", "-Command"); + } else { + actualShellCmd = ImmutableList.of("sh", "-c"); + } + assertEquals(actualShellCmd, shell); + } } From 5bed37703ec7b2ee623b19044ef3c38b169546c4 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Fri, 11 Sep 2015 17:31:21 +0900 Subject: [PATCH 4/4] Fix wrong Windows detection --- src/main/java/org/embulk/output/CommandFileOutputPlugin.java | 2 +- .../java/org/embulk/output/TestCommandFileOutputPlugin.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java index cd7a46e..691a24b 100644 --- a/src/main/java/org/embulk/output/CommandFileOutputPlugin.java +++ b/src/main/java/org/embulk/output/CommandFileOutputPlugin.java @@ -86,7 +86,7 @@ public List get() { public ShellFactory build() { String osName = System.getProperty("os.name"); - if(osName.contains("Windows")) { + if(osName.indexOf("Windows") >= 0) { this.shell = ImmutableList.of("PowerShell.exe", "-Command"); } else { this.shell = ImmutableList.of("sh", "-c"); diff --git a/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java b/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java index 7e38c2e..52ff848 100644 --- a/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java +++ b/src/test/java/org/embulk/output/TestCommandFileOutputPlugin.java @@ -29,7 +29,7 @@ public void testShell() { List shell = shellFactory.get(); String osName = System.getProperty("os.name"); List actualShellCmd; - if (osName.contains("Windows")) { + if (osName.indexOf("Windows") >= 0) { actualShellCmd = ImmutableList.of("PowerShell.exe", "-Command"); } else { actualShellCmd = ImmutableList.of("sh", "-c");