Skip to content

Commit

Permalink
Use Tempfile and File.read in ReleaseNotesUrlReader
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Sep 19, 2017
1 parent 055cc62 commit d077d45
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
25 changes: 15 additions & 10 deletions src/lib/y2packager/release_notes_url_reader.rb
Expand Up @@ -194,14 +194,17 @@ def release_notes_content_for_lang_and_format(lang, format)
return nil unless release_notes_index.empty? || indexed_release_notes_for?(lang, format)

# Where we want to store the downloaded release notes
filename = Yast::Builtins.sformat(
"%1/relnotes", Yast::SCR.Read(Yast::Path.new(".target.tmpdir"))
)
tmpfile = Tempfile.new("relnotes-")

return nil unless curl_download(release_notes_file_url(lang, format), filename)
return nil unless curl_download(release_notes_file_url(lang, format), tmpfile.path)

log.info("Release notes downloaded successfully")
Yast::SCR.Read(Yast::Path.new(".target.string"), filename)
File.read(tmpfile.path)
ensure
if tmpfile
tmpfile.close
tmpfile.unlink
end
end

# Determine whether the relnotes URL is valid
Expand Down Expand Up @@ -308,15 +311,14 @@ def indexed_release_notes_for?(user_lang, format)
def download_release_notes_index(url_base)
url_index = url_base + "/directory.yast"
log.info("Index with available files: #{url_index}")
filename = Yast::Builtins.sformat(
"%1/directory.yast", Yast::SCR.Read(Yast::Path.new(".target.tmpdir"))
)
tmpfile = Tempfile.new("directory.yast-")

# download the index with much shorter time-out
ok = curl_download(url_index, filename, max_time: 30)
ok = curl_download(url_index, tmpfile.path, max_time: 30)

if ok
log.info("Release notes index downloaded successfully")
index_file = File.read(filename)
index_file = File.read(tmpfile.path)
if index_file.nil? || index_file.empty?
log.info("Release notes index empty, not filtering further downloads")
nil
Expand All @@ -331,6 +333,9 @@ def download_release_notes_index(url_base)
log.info "Downloading index failed, trying all files according to selected language"
nil
end
ensure
tmpfile.close
tmpfile.unlink
end

# Download *url* to *filename*
Expand Down
19 changes: 12 additions & 7 deletions test/lib/release_notes_url_reader_test.rb
Expand Up @@ -9,22 +9,28 @@

let(:product) { instance_double(Y2Packager::Product, name: "dummy") }
let(:relnotes_url) { "http://doc.opensuse.org/openSUSE/release-notes-openSUSE.rpm" }
let(:content) { "Release Notes\n" }
let(:language) { double("Yast::Language", language: "de_DE") }
let(:curl_retcode) { 0 }
let(:relnotes_tmpfile) do
instance_double(Tempfile, path: "/tmp/relnotes", close: nil, unlink: nil)
end
let(:index_tmpfile) do
instance_double(Tempfile, path: "/tmp/directory.yast", close: nil, unlink: nil)
end

before do
allow(Yast::Pkg).to receive(:ResolvableProperties)
.with(product.name, :product, "").and_return(["relnotes_url" => relnotes_url])
allow(Yast::SCR).to receive(:Read).with(Yast::Path.new(".target.tmpdir"))
.and_return("/tmp")
allow(Yast::SCR).to receive(:Read).with(Yast::Path.new(".target.string"), /relnotes/)
.and_return("Release Notes\n")
allow(File).to receive(:read).with(/relnotes/).and_return(content)
allow(Yast::SCR).to receive(:Execute)
.with(Yast::Path.new(".target.bash"), /curl.*directory.yast/)
.and_return(1)
allow(Yast::SCR).to receive(:Execute)
.with(Yast::Path.new(".target.bash"), /curl.*RELEASE-NOTES/)
.and_return(curl_retcode)
allow(Tempfile).to receive(:new).with(/relnotes/).and_return(relnotes_tmpfile)
allow(Tempfile).to receive(:new).with(/directory.yast/).and_return(index_tmpfile)
described_class.clear_blacklist
described_class.enable!

Expand All @@ -41,12 +47,11 @@
cmd = %r{curl.*'http://doc.opensuse.org/openSUSE/RELEASE-NOTES.de_DE.txt'}
expect(Yast::SCR).to receive(:Execute).with(Yast::Path.new(".target.bash"), cmd)
.and_return(0)
expect(Yast::SCR).to receive(:Read).with(Yast::Path.new(".target.string"), /relnotes/)
.and_return("Release Notes\n")
expect(File).to receive(:read).with(/relnotes/).and_return(content)
rn = reader.release_notes(user_lang: "de_DE", format: :txt)

expect(rn.product_name).to eq("dummy")
expect(rn.content).to eq("Release Notes\n")
expect(rn.content).to eq(content)
expect(rn.user_lang).to eq("de_DE")
expect(rn.format).to eq(:txt)
expect(rn.version).to eq(:latest)
Expand Down

0 comments on commit d077d45

Please sign in to comment.