Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for windows #1828

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion digdag-docs/src/operators/sh.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ On Windows, you can set PowerShell.exe to the `shell` option:

_export:
sh:
shell: ["powershell.exe", "-"]
shell: ["powershell.exe"]

+step1:
sh>: step1.exe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,22 @@ private CommandStatus runCommand(final Config params, final CommandContext comma
{
final Path tempDir = workspace.createTempDir(String.format("digdag-sh-%d-", request.getTaskId()));
final Path workingDirectory = workspace.getPath(); // absolute
final Path runnerPath = tempDir.resolve("runner.sh"); // absolute
final Path runnerPath = tempDir.resolve(getScriptName()); // absolute

final List<String> shell;
if (params.has("shell")) {
shell = params.getListOrEmpty("shell", String.class);
List<String> temp_shell;
temp_shell = params.getListOrEmpty("shell", String.class);
shell = supportLegacyPowershellParm(temp_shell);
}
else {
shell = ImmutableList.of("/bin/sh");
shell = ImmutableList.of(getDefaultShell());
}

final ImmutableList.Builder<String> cmdline = ImmutableList.builder();
if (params.has("shell")) {
cmdline.addAll(shell);
}
else {
cmdline.addAll(shell);
}

cmdline.addAll(shell);

cmdline.add(workingDirectory.relativize(runnerPath).toString()); // relative

final String shScript = UserSecretTemplate.of(params.get("_command", String.class))
Expand Down Expand Up @@ -223,5 +222,30 @@ private CommandRequest buildCommandRequest(final CommandContext commandContext,
.ioDirectory(ioDirectory)
.build();
}

private List<String> supportLegacyPowershellParm (List<String> temp_shell)
{
if (temp_shell.get(0).toLowerCase().equals("powershell.exe") && temp_shell.size() == 2){
if (temp_shell.get(1).equals("-")){
temp_shell.remove(1);
logger.warn("Deprecated shell entry: [\"powershell.exe\", \"-\"]");
}
}
return temp_shell;
}
private String getScriptName()
{
return (isWindowsPlatform() ? "runner.ps1" : "runner.sh");
}
private String getDefaultShell()
{
return (isWindowsPlatform() ? "powershell.exe" : "/bin/sh");
}

private boolean isWindowsPlatform()
{
final String os = System.getProperty("os.name").toLowerCase();
return (os.startsWith("windows") ? true : false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package io.digdag.standards.operator;

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.omg.CORBA.ExceptionList;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.nio.file.Path;
import java.util.ArrayList;

import io.digdag.client.config.Config;
import io.digdag.client.config.ConfigFactory;
import io.digdag.core.DigdagEmbed;
import io.digdag.core.workflow.WorkflowTestingUtils;
import io.digdag.spi.TaskResult;
import io.digdag.standards.command.MockNonBlockingCommandExecutor;
import io.digdag.standards.operator.ShOperatorFactory;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


import static io.digdag.core.workflow.OperatorTestingUtils.newOperatorFactory;
import static io.digdag.core.workflow.OperatorTestingUtils.newTaskRequest;
import static io.digdag.core.workflow.OperatorTestingUtils.newContext;
import static io.digdag.core.workflow.WorkflowTestingUtils.loadYamlResource;
import static io.digdag.core.workflow.WorkflowTestingUtils.setupEmbed;
import static io.digdag.core.workflow.WorkflowTestingUtils.runWorkflow;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class ShOperatorFactoryTest
{
private static DigdagEmbed embed;

@BeforeClass
public static void createDigdagEmbed()
{
embed = WorkflowTestingUtils.setupEmbed();
}

@Rule
public TemporaryFolder folder = new TemporaryFolder();

private ObjectMapper mapper;
private MockNonBlockingCommandExecutor executor;
private Path tempPath;

@Before
public void createInstance1()
{
this.mapper = embed.getInjector().getInstance(ObjectMapper.class);
this.executor = new MockNonBlockingCommandExecutor(mapper);
this.tempPath = folder.getRoot().toPath();
//System.setProperty("os.name","windows");

}

@Test
public void testGetShellAndScriptnameOnWin()
throws Exception
{
System.setProperty("os.name","windows");

final String configResource = "/io/digdag/standards/operator/sh/basic.yml";
final ShOperatorFactory operatorFactory = new ShOperatorFactory(executor);
final ShOperatorFactory.ShOperator operator = (ShOperatorFactory.ShOperator) operatorFactory.newOperator(
newContext(tempPath, newTaskRequest().withConfig(loadYamlResource(configResource))));


Method method_isWindowsPlatform = ShOperatorFactory.ShOperator.class.getDeclaredMethod("isWindowsPlatform");
method_isWindowsPlatform.setAccessible(true);
assertTrue((boolean)method_isWindowsPlatform.invoke(operator));

Method method_getDefaultShell = ShOperatorFactory.ShOperator.class.getDeclaredMethod("getDefaultShell");
method_getDefaultShell.setAccessible(true);
assertTrue("powershell.exe" == (String)method_getDefaultShell.invoke(operator));

Method method_getScriptName = ShOperatorFactory.ShOperator.class.getDeclaredMethod("getScriptName");
method_getScriptName.setAccessible(true);
assertTrue("runner.ps1" == (String)method_getScriptName.invoke(operator));

System.clearProperty("os.name");
}


@Test
public void testGetShellAndScriptnameOnLinux()
throws Exception
{
System.setProperty("os.name","linux");

final String configResource = "/io/digdag/standards/operator/sh/basic.yml";
final ShOperatorFactory operatorFactory = new ShOperatorFactory(executor);
final ShOperatorFactory.ShOperator operator = (ShOperatorFactory.ShOperator) operatorFactory.newOperator(
newContext(tempPath, newTaskRequest().withConfig(loadYamlResource(configResource))));

Method method = ShOperatorFactory.ShOperator.class.getDeclaredMethod("isWindowsPlatform");
method.setAccessible(true);
assertFalse((boolean)method.invoke(operator));

Method method_getDefaultShell = ShOperatorFactory.ShOperator.class.getDeclaredMethod("getDefaultShell");
method_getDefaultShell.setAccessible(true);
assertTrue("/bin/sh" == (String)method_getDefaultShell.invoke(operator));

Method method_getScriptName = ShOperatorFactory.ShOperator.class.getDeclaredMethod("getScriptName");
method_getScriptName.setAccessible(true);
assertTrue("runner.sh" == (String)method_getScriptName.invoke(operator));

System.clearProperty("os.name");
}


@Test
public void testSupportLegacyPowershellParm()
throws Exception
{
final String configResource = "/io/digdag/standards/operator/sh/basic.yml";
final ShOperatorFactory operatorFactory = new ShOperatorFactory(executor);
final ShOperatorFactory.ShOperator operator = (ShOperatorFactory.ShOperator) operatorFactory.newOperator(
newContext(tempPath, newTaskRequest().withConfig(loadYamlResource(configResource))));

Method method = ShOperatorFactory.ShOperator.class.getDeclaredMethod("supportLegacyPowershellParm", List.class);
method.setAccessible(true);

List<String> legacy_powershell_parm = new ArrayList<String>(){{add("powershell.exe");add("-");}};
List<String> powershell_parm = (List)method.invoke(operator, legacy_powershell_parm);
assertTrue(1 == powershell_parm.size());
assertTrue("powershell.exe" == powershell_parm.get(0).toLowerCase());

}


}