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

Prevent empty ini files from being created and ignore them (fixes #356) #361

Merged
merged 3 commits into from
Jul 4, 2019
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
21 changes: 13 additions & 8 deletions lib/u3d/iniparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def load_ini(version, cached_versions, os: U3dCore::Helper.operating_system, off
ini_name = format(INI_NAME, version: version, os: os)
Utils.ensure_dir(default_ini_path)
ini_path = File.expand_path(ini_name, default_ini_path)
unless File.file?(ini_path)
unless File.file?(ini_path) && File.size(ini_path) > 0
raise "INI file does not exist at #{ini_path}" if offline
download_ini(version, cached_versions, os, ini_name, ini_path)
end
Expand All @@ -58,9 +58,8 @@ def create_linux_ini(version, size, url)
ini_name = format(INI_NAME, version: version, os: 'linux')
Utils.ensure_dir(default_ini_path)
ini_path = File.expand_path(ini_name, default_ini_path)
return if File.file? ini_path
File.open(ini_path, 'wb') do |f|
f.write %([Unity]
return if File.file?(ini_path) && File.size(ini_path) > 0
data = %([Unity]
; -- NOTE --
; This is not an official Unity file
; This has been created by u3d
Expand All @@ -69,7 +68,7 @@ def create_linux_ini(version, size, url)
size=#{size}
url=#{url}
)
end
write_ini_file(ini_path, data)
end

private
Expand All @@ -89,10 +88,16 @@ def download_ini(version, cached_versions, os, ini_name, ini_path)
end
uri = URI(cached_versions[version] + ini_name)
UI.verbose("Searching for ini file at #{uri}")

data = Net::HTTP.get(uri)
data.tr!("\"", '')
data.gsub!(/Note:.+\n/, '')

write_ini_file(ini_path, data)
end

def write_ini_file(ini_path, data)
File.open(ini_path, 'wb') do |f|
data = Net::HTTP.get(uri)
data.tr!("\"", '')
data.gsub!(/Note:.+\n/, '')
f.write(data)
end
end
Expand Down
39 changes: 39 additions & 0 deletions spec/u3d/iniparser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
f.write(ini_string)
f.rewind
allow(File).to receive(:file?) { true }
allow(File).to receive(:size) { ini_string.length }
allow(IniFile).to receive(:load).and_wrap_original { |m, _args| m.call(f.path) }
data = U3d::INIparser.load_ini('key', @cache, os: platform_os, offline: true)
expect(data['A']).not_to be_nil
Expand All @@ -57,6 +58,7 @@
it 'loads and filters broken Linux INI' do
broken_ini = 'spec/fixtures/unity-2017.3.0f1-linux.ini'
allow(File).to receive(:file?) { true }
allow(File).to receive(:size) { 1 }
allow(IniFile).to receive(:load).and_wrap_original { |m, _args| m.call(broken_ini) }

data = U3d::INIparser.load_ini('key', @cache, os: :linux, offline: true)
Expand All @@ -75,12 +77,31 @@
U3d::INIparser.load_ini('key', @cache, os: platform_os, offline: false)
end

it 'doesn\'t write the INI file from the web if it fails to download it' do
allow(File).to receive(:file?) { false }
allow(Net::HTTP).to receive(:get).with(anything).and_raise("network error")
expect(File).not_to receive(:open)
expect(IniFile).not_to receive(:load)
expect do
U3d::INIparser.load_ini('key', @cache, os: platform_os, offline: false)
end.to raise_error("network error")
end

it 'gets the INI file from the web if it is empty' do
allow(File).to receive(:file?) { true }
allow(File).to receive(:size) { 0 }
allow(IniFile).to receive(:load)
expect(Net::HTTP).to receive(:get) { '' }
U3d::INIparser.load_ini('key', @cache, os: platform_os, offline: false)
end

it 'parses and loads the INI data already existing without download' do
Tempfile.create(['temp', '.ini']) do |f|
ini_string = "[A]\ntest=initesting\n[B]\ntest=secondsection"
f.write(ini_string)
f.rewind
allow(File).to receive(:file?) { true }
allow(File).to receive(:size) { 1 }
allow(IniFile).to receive(:load).and_wrap_original { |m, _args| m.call(f.path) }
expect(Net::HTTP).not_to receive(:get)
data = U3d::INIparser.load_ini('key', @cache, os: platform_os, offline: false)
Expand Down Expand Up @@ -113,6 +134,7 @@
on_linux

allow(File).to receive(:file?).with(path) { true }
allow(File).to receive(:size) { 1 }
expect(File).to_not receive(:open)

U3d::INIparser.create_linux_ini('1.2.3f4', 12_345, 'http://example.com/')
Expand All @@ -134,6 +156,23 @@
U3d::INIparser.create_linux_ini('1.2.3f4', 12_345, 'http://example.com/')
end
end

context 'empty ini file' do
it 'writes the file' do
path = %r{Library\/Application Support\/u3d\/ini_files\/unity-1.2.3f4-linux.ini}

on_mac

allow(File).to receive(:file?).with(path) { true }
allow(File).to receive(:size) { 0 }
file = double('file')
allow(File).to receive(:open).with(path, 'wb').and_yield file

expect(file).to receive(:write).with(%r{\[Unity\](.*\n)+title=Unity\nsize=12345\nurl=http:\/\/example.com})

U3d::INIparser.create_linux_ini('1.2.3f4', 12_345, 'http://example.com/')
end
end
end
end
end