Merge pull request #408 from avdv/fix-invalid-encoding

Ensure to use `file_encoding` for arguments
This commit is contained in:
Claudio Bley 2020-10-31 22:07:19 +01:00 committed by GitHub
commit a7006a5fd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 3 deletions

View file

@ -12,7 +12,7 @@ module ColorLS
def initialize(input, all: false, report: false, sort: false, show: false,
mode: nil, git_status: false, almost_all: false, colors: [], group: nil,
reverse: false, hyperlink: false, tree_depth: nil)
@input = File.absolute_path(input)
@input = (+input).force_encoding(ColorLS.file_encoding)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@almost_all = almost_all
@ -33,7 +33,7 @@ module ColorLS
init_colors colors
@contents = init_contents(input)
@contents = init_contents(@input)
init_icons
end
@ -371,7 +371,7 @@ module ColorLS
end
def make_link(path, name)
href = "file://#{path}/#{name}"
href = "file://#{File.absolute_path(path)}/#{name}"
"\033]8;;#{href}\007#{name}\033]8;;\007"
end
end

View file

@ -0,0 +1,68 @@
# coding: utf-8
require 'spec_helper'
RSpec.describe ColorLS::Core do
subject { described_class.new(*args) }
context 'initialize' do
let(:args) { 'Imágenes' }
it 'works with Unicode characters' do
camera = 'Cámara'.force_encoding(ColorLS::file_encoding)
imagenes = 'Imágenes'.force_encoding(ColorLS::file_encoding)
dirInfo = instance_double(
'FileInfo',
:group => "sys",
:mtime => Time.now,
:directory? => true,
:owner => "user",
:name => imagenes,
:show => imagenes,
:nlink => 1,
:size => 128,
:blockdev? => false,
:chardev? => false,
:socket? => false,
:symlink? => false,
:stats => OpenStruct.new(
mode: 0o444, # read for user, owner, other
setuid?: false,
setgid?: false,
sticky?: false
),
:executable? => true
)
fileInfo = instance_double(
'FileInfo',
:group => "sys",
:mtime => Time.now,
:directory? => false,
:owner => "user",
:name => camera,
:show => camera,
:nlink => 1,
:size => 128,
:blockdev? => false,
:chardev? => false,
:socket? => false,
:symlink? => false,
:stats => OpenStruct.new(
mode: 0o444, # read for user, owner, other
setuid?: false,
setgid?: false,
sticky?: false
),
:executable? => false
)
expect(::Dir).to receive(:entries).and_return([camera])
allow(ColorLS::FileInfo).to receive(:new).and_return(dirInfo)
allow(ColorLS::FileInfo).to receive(:new).with(File.join(imagenes, camera), link_info: false) { fileInfo }
expect { subject }.not_to raise_error
end
end
end