forked from nikolavp/approval
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly implement the canApprove method in executable reporter
This uses 'where' on windows and 'type' on unix to check if the executable exists. In the future we might implement this by scanning the PATH variable on different platforms. closes nikolavp#14
- Loading branch information
Showing
10 changed files
with
181 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/nikolavp/approval/utils/ExecutableExistsOnPath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.nikolavp.approval.utils; | ||
|
||
/* | ||
* #%L | ||
* com.github.nikolavp:approval-core | ||
* %% | ||
* Copyright (C) 2014 Nikolavp | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import com.nikolavp.approval.reporters.ExecutableDifferenceReporter; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* An utility class that checks if a specified executable exists in your PATH environment variable. | ||
*/ | ||
public class ExecutableExistsOnPath extends CrossPlatformCommand<Boolean> { | ||
private final String executable; | ||
|
||
/** | ||
* Main constructor that acceptance the executable to check for. | ||
* | ||
* @param executable the executable name | ||
*/ | ||
public ExecutableExistsOnPath(String executable) { | ||
this.executable = executable; | ||
} | ||
|
||
@Override | ||
protected Boolean onWindows() { | ||
try { | ||
return ExecutableDifferenceReporter.runProcess("where " + executable) | ||
.waitFor() == 0; | ||
} catch (InterruptedException e) { | ||
return Boolean.FALSE; | ||
} catch (IOException e) { | ||
return Boolean.FALSE; | ||
} | ||
} | ||
|
||
@Override | ||
protected Boolean onUnix() { | ||
try { | ||
//Type is the mostl portable way to check this. Some shells that implement POSIX don't implement the where or which builtin | ||
//http://stackoverflow.com/questions/4781772/how-to-test-if-an-executable-exists-in-the-path-from-a-windows-batch-file | ||
return ExecutableDifferenceReporter.runProcess("/bin/sh", "-c", "type " + executable) | ||
.waitFor() == 0; | ||
} catch (InterruptedException e) { | ||
return Boolean.FALSE; | ||
} catch (IOException e) { | ||
return Boolean.FALSE; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/com/nikolavp/approval/utils/ExecutableExistsOnPathTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.nikolavp.approval.utils; | ||
|
||
/* | ||
* #%L | ||
* com.github.nikolavp:approval-core | ||
* %% | ||
* Copyright (C) 2014 Nikolavp | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Assert; | ||
import org.junit.Assume; | ||
import org.junit.Test; | ||
|
||
/** | ||
* User: nikolavp (Nikola Petrov) Date: 14-9-1 Time: 8:39 | ||
*/ | ||
public class ExecutableExistsOnPathTest { | ||
@Test | ||
public void shouldProperlyExecuteOnWindows() throws Exception { | ||
CrossPlatformCommand.setOS("Windows NT"); | ||
Assert.assertThat(new ExecutableExistsOnPath("non-existing-executable").execute(), CoreMatchers.equalTo(false)); | ||
} | ||
|
||
@Test | ||
public void shouldProperlyExecuteOnUnix() throws Exception { | ||
CrossPlatformCommand.setOS("Linux"); | ||
Assert.assertThat(new ExecutableExistsOnPath("non-existing-executable").execute(), CoreMatchers.equalTo(false)); | ||
|
||
Assume.assumeTrue(CrossPlatformCommand.isUnix()); | ||
Assert.assertThat(new ExecutableExistsOnPath("vim").execute(), CoreMatchers.equalTo(true)); | ||
} | ||
} |