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

Shim IO#pread when not supported #52

Merged
merged 2 commits into from
Aug 8, 2023
Merged
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
13 changes: 11 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,35 @@ on:

jobs:
build:
runs-on: ubuntu-latest
name: "Ruby ${{ matrix.ruby }} / Failure allowed: ${{ matrix.experimental }}"
runs-on: ${{ matrix.os }}-latest
name: "Ruby ${{ matrix.ruby }} / ${{ matrix.os }} / Failure allowed: ${{ matrix.experimental }}"
continue-on-error: ${{ matrix.experimental }}
timeout-minutes: 5

strategy:
fail-fast: false
matrix:
os: ["ubuntu"]
ruby: ["2.6", "2.7", "3.0", "3.1", "3.2"]
experimental: [false]
include:
- ruby: "3.2"
os: "windows"
experimental: false
- ruby: "ruby-head"
os: "ubuntu"
experimental: true
- ruby: "truffleruby-head"
os: "ubuntu"
experimental: true
- ruby: "jruby-head"
os: "ubuntu"
experimental: true
- ruby: "jruby-9.3.9.0"
os: "ubuntu"
experimental: true
- ruby: "jruby-9.4.0.0"
os: "ubuntu"
experimental: true
steps:
- uses: actions/checkout@v3
Expand Down
30 changes: 28 additions & 2 deletions lib/mini_mime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,40 @@ def fetch(key, &blk)
end
end

if ::File.method_defined?(:pread)
PReadFile = ::File
else
# For Windows support
class PReadFile
def initialize(filename)
@mutex = Mutex.new
# We must open the file in binary mode
# otherwise Ruby's automatic line terminator
# translation will skew the row size
@file = ::File.open(filename, 'rb')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so it's because for IOs in text mode, Ruby automatically replace \r\n by \n, so the row_length end up being off by one.

end

def readline(*args)
@file.readline(*args)
end

def pread(size, offset)
@mutex.synchronize do
@file.seek(offset, IO::SEEK_SET)
@file.read(size)
end
end
end
end

class RandomAccessDb
MAX_CACHED = 100

def initialize(path, sort_order)
@path = path
@file = File.open(@path)
@file = PReadFile.new(@path)

@row_length = @file.readline.length
@row_length = @file.readline("\n").length
@file_length = File.size(@path)
@rows = @file_length / @row_length

Expand Down
64 changes: 63 additions & 1 deletion test/mini_mime_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,64 @@ def should_prioritize_extensions_correctly
end

if defined? MIME::Types
WINDOWS_TYPES = {
"cu" => "application/cu-seeme",
"ecma" => "application/ecmascript",
"es" => "application/ecmascript",
"jar" => "application/java-archive",
"ser" => "application/java-serialized-object",
"mp4" => "application/mp4",
"mpg4" => "application/mp4",
"doc" => "application/msword",
"pgp" => "application/octet-stream",
"gpg" => "application/octet-stream",
"ai" => "application/pdf",
"asc" => "application/pgp-signature",
"rtf" => "application/rtf",
"spp" => "application/scvp-vp-response",
"sgml" => "application/sgml",
"curl" => "application/vnd.curl",
"odc" => "application/vnd.oasis.opendocument.chart",
"odf" => "application/vnd.oasis.opendocument.formula",
"odi" => "application/vnd.oasis.opendocument.image",
"bdm" => "application/vnd.syncml.dm+wbxml",
"dcr" => "application/x-director",
"exe" => "application/x-ms-dos-executable",
"wmz" => "application/x-ms-wmz",
"cmd" => "application/x-msdos-program",
"bat" => "application/x-msdos-program",
"com" => "application/x-msdos-program",
"reg" => "application/x-msdos-program",
"ps1" => "application/x-msdos-program",
"vbs" => "application/x-msdos-program",
"pm" => "application/x-pagemaker",
"xml" => "application/xml",
"dtd" => "application/xml-dtd",
"kar" => "audio/midi",
"mid" => "audio/midi",
"midi" => "audio/midi",
"m4a" => "audio/mp4",
"mp2" => "audio/mpeg",
"ogg" => "audio/ogg",
"wav" => "audio/wav",
"webm" => "audio/webm",
"wmv" => "audio/x-ms-wmv",
"ra" => "audio/x-pn-realaudio",
"hif" => "image/heic",
"sub" => "image/vnd.dvb.subtitle",
"xbm" => "image/x-xbitmap",
"mts" => "model/vnd.mts",
"rst" => "text/plain",
}

def test_full_parity_with_mime_types
exts = Set.new
MIME::Types.each do |type|
type.extensions.each { |ext| exts << ext }
end

differences = []

exts.each do |ext|
types = MIME::Types.type_for("a.#{ext}")

Expand All @@ -73,8 +125,18 @@ def test_full_parity_with_mime_types
# puts "#{ext} Expected #{type.content_type} Got #{MiniMime.lookup_by_filename("a.#{ext}").content_type}"
# end

assert_equal type.content_type, MiniMime.lookup_by_filename("a.#{ext}").content_type
expected = type.content_type
if WINDOWS_TYPES.key?(ext) && RUBY_PLATFORM.match?(/mingw|windows/i)
expected = WINDOWS_TYPES[ext]
end
actual = MiniMime.lookup_by_filename("a.#{ext}").content_type

if expected != actual
differences << %{Expected ".#{ext}" to return #{expected.inspect}, got: #{actual.inspect}}
end
end

assert differences.empty?, differences.join("\n")
end
end
end