Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7b7847167f62
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 5717e3511910
Choose a head ref
  • 3 commits
  • 3 files changed
  • 2 contributors

Commits on Aug 22, 2016

  1. Change script file restriction

    Daniel Smith committed Aug 22, 2016
    Copy the full SHA
    368c987 View commit details
  2. Remove maven-metadata files

    Daniel Smith committed Aug 22, 2016
    Copy the full SHA
    8fb74e4 View commit details

Commits on Aug 24, 2016

  1. Merge pull request #4100 from jellymann/fix-stdin-bug

    Change script file restriction
    enebo authored Aug 24, 2016
    Copy the full SHA
    5717e35 View commit details
2 changes: 1 addition & 1 deletion core/src/main/java/org/jruby/RubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -398,7 +398,7 @@ public InputStream getScriptSource() {
final String script = getScriptFileName();
FileResource resource = JRubyFile.createRestrictedResource(getCurrentDirectory(), getScriptFileName());
if (resource != null && resource.exists()) {
if (resource.isFile() || resource.isSymLink()) {
if (resource.canRead() && !resource.isDirectory()) {
if (isXFlag()) {
// search for a shebang line and
// return the script between shebang and __END__ or CTRL-Z (0x1A)
72 changes: 70 additions & 2 deletions core/src/test/java/org/jruby/test/TestRubyInstanceConfig.java
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
* rights and limitations under the License.
*
* Copyright (C) 2008 Ola Bini <ola.bini@gmail.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
@@ -27,6 +27,11 @@
***** END LICENSE BLOCK *****/
package org.jruby.test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Locale;

import org.jruby.exceptions.MainExitException;
import org.jruby.Ruby;
import org.jruby.RubyInstanceConfig;
import org.jruby.runtime.load.LoadService;
@@ -38,7 +43,7 @@ public class TestRubyInstanceConfig extends TestRubyBase {
public TestRubyInstanceConfig(String name) {
super(name);
}

private RubyInstanceConfig config;

@Override
@@ -74,4 +79,67 @@ public LoadService create(Ruby runtime) {
assertTrue(called[0]);
assertEquals(NullLoadService.class, ruby.getLoadService().getClass());
}

public void testGetScriptSource() throws Exception {
config.setCurrentDirectory("uri:classloader:/test_dir");
config.setScriptFileName("test_script.rb");

String scriptSource = inputStreamToString(config.getScriptSource());

assertEquals(scriptSource, "puts \"Hello World\"\n");
}

public void testGetScriptSourceWithSTDIN() throws Exception {
config.setScriptFileName(getSTDINPath());

assertNotNull(config.getScriptSource());
}

public void testGetScriptSourceWithDirectory() throws Exception {
config.setCurrentDirectory("uri:classloader:/somedir");
config.setScriptFileName("dir_with_listing");

try {
config.getScriptSource();
fail("Should throw FileNotFoundException");
} catch (MainExitException ex) {
assertTrue(ex.getMessage().indexOf("(Not a file)") > -1);
}
}

public void testGetScriptSourceWithNonexistentFile() throws Exception {
config.setCurrentDirectory("uri:classloader:/somedir");
config.setScriptFileName("non_existing.rb");

try {
config.getScriptSource();
fail("Should throw FileNotFoundException");
} catch (MainExitException ex) {
assertTrue(ex.getMessage().indexOf("(No such file or directory)") > -1);
}
}

private String inputStreamToString(InputStream inputStream) throws Exception {
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}
return result.toString("UTF-8");
}

private String getSTDINPath() {
String osName = System.getProperty("os.name").toLowerCase(Locale.US);
if (osName.indexOf("windows") > -1) {
return "CON";
}
if (osName.indexOf("openvms") > -1) {
return "/sys$input";
}
if (osName.indexOf("mac") > -1) {
return "/dev/fd/0";
}
return "/dev/stdin";
}
}
1 change: 1 addition & 0 deletions core/src/test/resources/test_dir/test_script.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
puts "Hello World"