diff --git a/README.md b/README.md index b027718..4e255f1 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,6 @@ writespdx(jsonDoc, "jsonread.spdx") # Write SPDX document in JSON format writespdx(tvDoc, "tagvalueread.spdx.json") - ``` ## Creating and populating/modifying a new SPDX document diff --git a/src/api.jl b/src/api.jl index 9df4d95..f5f09ce 100644 --- a/src/api.jl +++ b/src/api.jl @@ -5,88 +5,88 @@ export addcreator!, getcreators, deletecreator!, setcreationtime! export readspdx, writespdx ######################## -function printJSON(doc::SpdxDocumentV2, fname::AbstractString) - open(fname, "w") do f - jsonDoc= convert_to_JSON(doc, SpdxDocumentV2_NameTable) - JSON.print(f, jsonDoc, 4) - end +function printJSON(io::IO, doc::SpdxDocumentV2) + jsonDoc= convert_to_JSON(doc, SpdxDocumentV2_NameTable) + JSON.print(io, jsonDoc, 4) + return nothing end ######################### -function printTagValue(doc::SpdxDocumentV2, fname::AbstractString) - TagValueDoc= IOBuffer() - open(fname, "w") do f - convert_doc_to_TagValue!(TagValueDoc, doc, SpdxDocumentV2_NameTable) - write(f, take!(TagValueDoc)) - end +function printTagValue(io::IO, doc::SpdxDocumentV2) + convert_doc_to_TagValue!(io, doc, SpdxDocumentV2_NameTable) return nothing end ######################### -function readJSON(fname::AbstractString) - JSONfile= JSON.parsefile(fname) +function readJSON(io::IO) + JSONfile= JSON.parse(io) doc= convert_from_JSON(JSONfile, SpdxDocumentV2_NameTable, SpdxDocumentV2) return doc end ######################### -function readTagValue(fname::AbstractString) - doc= nothing - open(fname) do TVfile - doc= parse_TagValue(TVfile, SpdxDocumentV2_NameTable, SpdxDocumentV2) - end +function readTagValue(io::IO) + doc= parse_TagValue(io, SpdxDocumentV2_NameTable, SpdxDocumentV2) return doc end ######################### +function readspdx(io::IO; format::AbstractString) + doc= if format == "JSON" + readJSON(io) + elseif format == "TagValue" + readTagValue(io) + else + error("Specified Format ", format, " is not supported") + end + + return doc +end + function readspdx(fname::AbstractString; format::Union{AbstractString, Nothing}=nothing) - if !isnothing(format) - if format=="JSON" - fext= ".json" - elseif format=="TagValue" - fext= ".spdx" + if isnothing(format) + fext= last(splitext(fname)) + format= if fext == ".json" + "JSON" + elseif fext == ".spdx" + "TagValue" else - error("Specified Format ", format, " is not supported") + error("File format ", fext, " is not supported") end - else - temp= splitext(fname) - if !in(temp[2], (".json", ".spdx")) - error("File format ", temp[2], " is not supported") - end - fext= temp[2] end - if fext == ".json" - doc= readJSON(fname) - elseif fext == ".spdx" - doc= readTagValue(fname) + doc= open(fname) do io + readspdx(io; format) end return doc end ######################### +function writespdx(io::IO, doc::SpdxDocumentV2; format::AbstractString) + if format == "JSON" + printJSON(io, doc) + elseif format == "TagValue" + printTagValue(io, doc) + else + error("Specified Format ", format, " is not supported") + end +end + function writespdx(doc::SpdxDocumentV2, fname::AbstractString; format::Union{AbstractString, Nothing}=nothing) - if !isnothing(format) - if format=="JSON" - fext= ".json" - elseif format=="TagValue" - fext= ".spdx" + if isnothing(format) + fext= last(splitext(fname)) + format= if fext == ".json" + "JSON" + elseif fext == ".spdx" + "TagValue" else - error("Specified Format ", format, " is not supported") - end - else - temp= splitext(fname) - if !in(temp[2], (".json", ".spdx")) - error("File format ", temp[2], " is not supported") + error("File format ", fext, " is not supported") end - fext= temp[2] end - if fext == ".json" - printJSON(doc, fname) - elseif fext == ".spdx" - printTagValue(doc, fname) + open(fname, "w") do io + writespdx(io, doc; format) end end @@ -143,4 +143,4 @@ end function Base.hash(A::AbstractSpdx, h::UInt) return _hash(A, h) -end \ No newline at end of file +end diff --git a/test/test_read_write.jl b/test/test_read_write.jl index 16ab80e..22f7127 100644 --- a/test/test_read_write.jl +++ b/test/test_read_write.jl @@ -9,17 +9,25 @@ @test spdxDoc isa SpdxDocumentV2 testdir= mktempdir() - @testset "JSON format roundtrips" begin + @testset "JSON format I/O roundtrips" begin + io = IOBuffer() + @test_throws "Specified Format YAML is not supported" writespdx(io, spdxDoc; format= "YAML") + @test_throws UndefKeywordError writespdx(io, spdxDoc) + writespdx(io, spdxDoc; format= "JSON") + rt_spdx = readspdx(seekstart(io); format= "JSON") + @test isequal(spdxDoc, rt_spdx) + + @test isequal(spdxDoc, readJSON(seekstart(io))) + @test_throws Exception readTagValue(seekstart(io)) + end + + @testset "JSON format file roundtrips" begin rt_path = joinpath(testdir, "out.spdx.json") - @test_throws "Specified Format YAML is not supported" writespdx(spdxDoc, rt_path; format= "YAML") @test_throws "File format .yml is not supported" writespdx(spdxDoc, replace(rt_path, "json" => "yml")) writespdx(spdxDoc, rt_path) rt_spdx = readspdx(rt_path) @test isequal(spdxDoc, rt_spdx) - @test isequal(spdxDoc, readJSON(rt_path)) - @test_throws Exception readTagValue(rt_path) - # Change the describes relationship in the SPDX document to get more code coverage spdxDoc2 = readspdx(joinpath(spdxdir, "SPDX.spdx.json")) idx= findfirst(x -> isequal(x.RelationshipType, "DESCRIBES"), spdxDoc2.Relationships) @@ -45,16 +53,19 @@ temp= readspdx(joinpath(spdxdir, "test/SPDX_badfields.spdx.json")); @test temp isa SpdxDocumentV2 end - - @testset "TagValue format roundtrips" begin - rt_path = joinpath(testdir, "out.spdx") - writespdx(spdxDoc, rt_path) - rt_spdx = readspdx(rt_path) + + @testset "TagValue format I/O roundtrips" begin + io = IOBuffer() + writespdx(io, spdxDoc; format= "TagValue") + rt_spdx = readspdx(seekstart(io); format= "TagValue") @test isequal(spdxDoc, rt_spdx) - @test isequal(spdxDoc, readTagValue(rt_path)) - @test_throws Exception readJSON(rt_path) + @test isequal(spdxDoc, readTagValue(seekstart(io))) + @test_throws Exception readJSON(seekstart(io)) + end + @testset "TagValue format file roundtrips" begin + rt_path = joinpath(testdir, "out.spdx") # Write and read a second SPDX file for more coverage. spdxDoc2= c # from build_testDocument.jl writespdx(spdxDoc2, rt_path; format= "TagValue")