|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved. This |
| 3 | + * code is released under a tri EPL/GPL/LGPL license. You can use it, |
| 4 | + * redistribute it and/or modify it under the terms of the: |
| 5 | + * |
| 6 | + * Eclipse Public License version 1.0, or |
| 7 | + * GNU General Public License version 2, or |
| 8 | + * GNU Lesser General Public License version 2.1. |
| 9 | + */ |
| 10 | +package org.truffleruby; |
| 11 | + |
| 12 | +import com.oracle.truffle.api.TruffleFile; |
| 13 | +import com.oracle.truffle.api.TruffleLanguage; |
| 14 | +import org.junit.Test; |
| 15 | +import org.truffleruby.RubyTest; |
| 16 | +import org.truffleruby.shared.TruffleRuby; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.charset.Charset; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.StandardOpenOption; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | +import java.util.function.Consumer; |
| 27 | +import org.graalvm.polyglot.Source; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertNotEquals; |
| 31 | +import org.truffleruby.language.RubyRootNode; |
| 32 | + |
| 33 | +public class RubyFileTypeDetectorTest extends RubyTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testDirect() { |
| 37 | + final RubyFileTypeDetector fileTypeDetector = new RubyFileTypeDetector(); |
| 38 | + testWithAST("", new Consumer<RubyRootNode>() { |
| 39 | + @Override |
| 40 | + public void accept(RubyRootNode rootNode) { |
| 41 | + TruffleLanguage.Env env = rootNode.getContext().getEnv(); |
| 42 | + try { |
| 43 | + for (TestCase testCase : getTestCases()) { |
| 44 | + TruffleFile file = env.getTruffleFile(testCase.path.toString()); |
| 45 | + if (testCase.hasRubyMimeType) { |
| 46 | + assertEquals(TruffleRuby.MIME_TYPE, fileTypeDetector.findMimeType(file)); |
| 47 | + } else { |
| 48 | + assertNotEquals(TruffleRuby.MIME_TYPE, fileTypeDetector.findMimeType(file)); |
| 49 | + } |
| 50 | + } |
| 51 | + } catch (IOException ioe) { |
| 52 | + throw new RuntimeException(ioe); |
| 53 | + } |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testIndirect() throws IOException { |
| 60 | + for (TestCase testCase : getTestCases()) { |
| 61 | + if (testCase.hasRubyMimeType) { |
| 62 | + assertEquals(TruffleRuby.MIME_TYPE, Source.findMimeType(testCase.path.toFile())); |
| 63 | + } else { |
| 64 | + assertNotEquals(TruffleRuby.MIME_TYPE, Source.findMimeType(testCase.path.toFile())); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testEncoding() { |
| 71 | + final RubyFileTypeDetector fileTypeDetector = new RubyFileTypeDetector(); |
| 72 | + testWithAST("", new Consumer<RubyRootNode>() { |
| 73 | + @Override |
| 74 | + public void accept(RubyRootNode rootNode) { |
| 75 | + TruffleLanguage.Env env = rootNode.getContext().getEnv(); |
| 76 | + try { |
| 77 | + for (TestCase testCase : getTestCases()) { |
| 78 | + TruffleFile file = env.getTruffleFile(testCase.path.toString()); |
| 79 | + if (testCase.hasRubyMimeType) { |
| 80 | + assertEquals(TruffleRuby.MIME_TYPE, fileTypeDetector.findMimeType(file)); |
| 81 | + } else { |
| 82 | + assertNotEquals(TruffleRuby.MIME_TYPE, fileTypeDetector.findMimeType(file)); |
| 83 | + } |
| 84 | + } |
| 85 | + } catch (IOException ioe) { |
| 86 | + throw new RuntimeException(ioe); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + private static TestCase[] getTestCases() throws IOException { |
| 93 | + final Path tempDirectory = Files.createTempDirectory("truffleruby"); |
| 94 | + tempDirectory.toFile().deleteOnExit(); |
| 95 | + |
| 96 | + final List<TestCase> testCases = new ArrayList<>(); |
| 97 | + |
| 98 | + testCases.add(new TestCase(createFile(tempDirectory, "test.rb", "puts 'hello'"), true, null)); |
| 99 | + testCases.add(new TestCase(createFile(tempDirectory, "TESTUP.RB", "puts 'hello'"), true, null)); |
| 100 | + testCases.add(new TestCase(createFile(tempDirectory, "Gemfile", "puts 'hello'"), true, null)); |
| 101 | + testCases.add(new TestCase(createFile(tempDirectory, "Rakefile", "puts 'hello'"), true, null)); |
| 102 | + testCases.add(new TestCase(createFile(tempDirectory, "Mavenfile", "puts 'hello'"), true, null)); |
| 103 | + testCases.add(new TestCase(createFile(tempDirectory, "test.rake", "puts 'hello'"), true, null)); |
| 104 | + testCases.add(new TestCase(createFile(tempDirectory, "test.gemspec", "puts 'hello'"), true, null)); |
| 105 | + testCases.add(new TestCase(createFile(tempDirectory, "shebang", "#!/usr/bin/ruby\nputs 'hello'"), true, null)); |
| 106 | + testCases.add(new TestCase(createFile(tempDirectory, "env-shebang", "#!/usr/bin/env ruby\nputs 'hello'"), true, null)); |
| 107 | + testCases.add(new TestCase(createFile(tempDirectory, "test.norb", "# encoding: UTF-8\nputs 'hello'"), false, null)); |
| 108 | + testCases.add(new TestCase(createFile(tempDirectory, "encoding1.rb", "# encoding: UTF-8\nputs 'hello'"), true, StandardCharsets.UTF_8)); |
| 109 | + testCases.add(new TestCase(createFile(tempDirectory, "encoding2.rb", "# coding: UTF-8\nputs 'hello'"), true, StandardCharsets.UTF_8)); |
| 110 | + testCases.add(new TestCase(createFile(tempDirectory, "encoding3.rb", "# -*- coding: UTF-8 -*-\nputs 'hello'"), true, StandardCharsets.UTF_8)); |
| 111 | + testCases.add(new TestCase(createFile(tempDirectory, "shebang-encoding", "#!/usr/bin/ruby\n# encoding: UTF-8\nputs 'hello'"), true, StandardCharsets.UTF_8)); |
| 112 | + testCases.add(new TestCase(createFile(tempDirectory, "env-shebang-encoding", "#!/usr/bin/env ruby\n# encoding: UTF-8\nputs 'hello'"), true, StandardCharsets.UTF_8)); |
| 113 | + return testCases.toArray(new TestCase[testCases.size()]); |
| 114 | + } |
| 115 | + |
| 116 | + private static Path createFile(Path parent, String name, String contents) throws IOException { |
| 117 | + final Path file = Files.createFile(parent.resolve(name)); |
| 118 | + Files.write(file, contents.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE); |
| 119 | + return file; |
| 120 | + } |
| 121 | + |
| 122 | + private static final class TestCase { |
| 123 | + final Path path; |
| 124 | + final boolean hasRubyMimeType; |
| 125 | + final Charset encoding; |
| 126 | + |
| 127 | + private TestCase(Path path, boolean hasRubyMimeType, Charset encoding) { |
| 128 | + this.path = path; |
| 129 | + this.hasRubyMimeType = hasRubyMimeType; |
| 130 | + this.encoding = encoding; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments