Skip to content

Commit

Permalink
capture stdout from binaries rather than suppress
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Doolittle committed May 27, 2020
1 parent 6f72487 commit 5598fb8
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 34 deletions.
32 changes: 19 additions & 13 deletions src/valid_subroutines.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
"""
run_valid(method_flag::String, args::Array{String,1}; verbose::Bool=false)
run_valid( method_flag::String, args::Array{String,1}; verbose::Bool=false ) :: String
!!! warning
This method is intended for advanced use of the `valid` binary. User knowledge
of flags and arguments is required for successful execution. Users
must explicitly handle file IO for the `valid` binary.
Runs the `valid` binary through `PORTA_jll`. The `method_flag` argument tells the `valid`
binary which subroutine to call. Valid options include:
Runs the `valid` binary through `PORTA_jll` and returns a string containing `STDOUT`.
The `method_flag` specifies which `valid` subroutine to call.
Valid options for `method_flag` are:
* `"-C"` runs the `fctp` subroutine
* `"-I"` runs the `iespo` subroutine
* `"-P"` runs the `posie` subroutine
* `"-V"` runs the `vint` subroutine
The `args` parameter is uniquely specified by `method_flag`, for more information
regarding methods and arguments see the [valid documentation](https://github.com/bdoolittle/julia-porta#valid).
regarding methods and arguments see the [`valid` documentation](https://github.com/bdoolittle/julia-porta#valid).
The `verbose` argument determines whether the `valid` binary prints to `STDOUT`.
If `verbose=true` the `valid` binary prints to `STDOUT`.
"""
function run_valid(method_flag::String, args::Array{String,1}; verbose::Bool=false)
function run_valid(method_flag::String, args::Array{String,1}; verbose::Bool=false) :: String
if !(method_flag in ["-C", "-I", "-P", "-V"])
throw(DomainError(method_flag, "method_flag is invalid. Valid options are \"-C\", \"-I\", \"-P\", \"-V\"."))
end

valid() do valid_path
if verbose
run(`$valid_path $method_flag $args`)
else
@suppress run(`$valid_path $method_flag $args`)
end
stdout = valid() do valid_path
@capture_out run(`$valid_path $method_flag $args`)
end

if verbose
print(stdout)
end

stdout
end

"""
Expand Down Expand Up @@ -118,7 +122,9 @@ function fctp( inequalities::PortaMatrix, poi::POI;
ieq_filepath = write_ieq(filename, IEQ(inequalities=inequalities), dir=workdir)
poi_filepath = write_poi(filename, poi, dir=workdir)

run_valid("-C", [ieq_filepath, poi_filepath], verbose=verbose)
# there exists an error "sh: dim: command not found" coming from PORTA
# TODO: fix PORTA error in source code.
@suppress_err run_valid("-C", [ieq_filepath, poi_filepath], verbose=verbose)

poi_files = filter(file -> occursin(r"^.*\.ieq\d+\.poi$", file), readdir(workdir))

Expand Down
57 changes: 40 additions & 17 deletions src/xporta_subroutines.jl
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
"""
run_xporta( method_flag::String, args::Array{String,1}; verbose::Bool = false)
run_xporta( method_flag::String, args::Array{String,1}; verbose::Bool = false) :: String
!!! warning
This method is intended for advanced use of the xporta binary. User knowledge
of flags and arguments is required for successful execution. Furthermore, users
must explicitly handle file IO for the xporta binary.
must explicitly handle file IO for the `xporta` binary.
Runs the xporta binary through `PORTA_jll`. The `method_flag` argument tells the xporta
binary which method to call. Valid options include:
* `"-D"` runs the `dim` method
* `"-F"` runs the `fmel` method
* `"-S"` runs the `portsort` method
* `"-T"` runs the `traf` method
Runs the `xporta` binary through `PORTA_jll` and returns a string containing `STDOUT`.
The `method_flag` argument specifies which `xporta` subroutine to call.
Valid options for `method_flag` are:
* `"-D"` runs the `dim` subroutine
* `"-F"` runs the `fmel` subroutine
* `"-S"` runs the `portsort` subroutine
* `"-T"` runs the `traf` subroutine
The `args` parameter is uniquely specified by `method_flag`, for more information
regarding methods and arguments see the [xporta documentation](https://github.com/bdoolittle/julia-porta/blob/master/README.md#xporta).
regarding methods and arguments see the [`xporta` documentation](https://github.com/bdoolittle/julia-porta#xporta).
The `verbose` argument determines whether the xporta prints to `STDOUT`.
If `verbose=true` the `xporta` prints to `STDOUT`.
"""
function run_xporta(method_flag::String, args::Array{String,1}; verbose::Bool=false)
function run_xporta(method_flag::String, args::Array{String,1}; verbose::Bool=false) :: String
if !(method_flag in ["-D", "-F", "-S", "-T"])
throw(DomainError(method_flag, "method_flag is invalid. Valid options are \"-D\", \"-F\", \"-S\", \"-T\"."))
end

xporta() do xporta_path
if !verbose
@suppress run(`$xporta_path $method_flag $args`)
else
run(`$xporta_path $method_flag $args`)
end
stdout = xporta() do xporta_path
@capture_out run(`$xporta_path $method_flag $args`)
end

if verbose
print(stdout)
end

stdout
end

"""
Expand Down Expand Up @@ -203,3 +207,22 @@ function portsort(poi::POI;

return poi
end

function dim(poi::POI;
dir::String="./",
filename::String="dim_tmp",
cleanup::Bool=true,
verbose::Bool=false
)

workdir = cleanup ? make_porta_tmp(dir) : dir

poi_filepath = write_poi(filename, poi, dir=workdir)

run_xporta("-D", ["-p", poi_filepath], verbose=verbose)

if cleanup
rm_porta_tmp(dir)
end

end
5 changes: 5 additions & 0 deletions test/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
[[Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[Suppressor]]
git-tree-sha1 = "a819d77f31f83e5792a76081eee1ea6342ab8787"
uuid = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
version = "0.2.0"

[[Test]]
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
27 changes: 25 additions & 2 deletions test/integration/valid_subroutines.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Test, XPORTA
using Test, Suppressor, XPORTA

@testset "src/valid_subroutines.jl" begin

Expand All @@ -8,6 +8,30 @@ dir = "./test/files/"
@testset "throws DomainError if method_flag is not recognized" begin
@test_throws DomainError XPORTA.run_valid("-X", [dir*"example1.poi"])
end

@testset "verbose prints to STDOUT" begin
# setup files for first run
XPORTA.rm_porta_tmp(dir)
XPORTA.make_porta_tmp(dir)
ex1_poi_filepath = cp(dir*"example1.poi", dir*"porta_tmp/example1.poi")
ex1_ieq_filepath = cp(dir*"example1.ieq", dir*"porta_tmp/example1.ieq")

# stdout is returned when verbose=false (also returned when true)
return_string = XPORTA.run_valid("-P", [ex1_ieq_filepath, ex1_poi_filepath], verbose=false)

# setup files for second run
XPORTA.rm_porta_tmp(dir)
XPORTA.make_porta_tmp(dir)
ex1_poi_filepath = cp(dir*"example1.poi", dir*"porta_tmp/example1.poi")
ex1_ieq_filepath = cp(dir*"example1.ieq", dir*"porta_tmp/example1.ieq")

# capturing stdout when verbose=true
capture_string = @capture_out XPORTA.run_valid("-P", [ex1_ieq_filepath, ex1_poi_filepath], verbose=true)

@test return_string == capture_string

XPORTA.rm_porta_tmp(dir)
end
end

@testset "posie()" begin
Expand Down Expand Up @@ -281,7 +305,6 @@ end
end
end


@testset "open linear system" begin
@testset "positive octant in 3-space" begin
# bounds completely specify a valid range w/o inequalities
Expand Down
33 changes: 31 additions & 2 deletions test/integration/xporta_subroutines.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
using Test, XPORTA
using Test, Suppressor, XPORTA

@testset "src/xporta_subroutines.jl" begin

dir = "./test/files/"

@testset "run_xporta()" begin
@testset "throws DomainError if method_flag is not recognized" begin
@test_throws DomainError XPORTA.run_xporta("-X", ["dir/example1.poi"])
@test_throws DomainError XPORTA.run_xporta("-X", [dir *"example1.poi"])
end

@testset "verbose prints to STDOUT" begin
# setup files for first run
XPORTA.rm_porta_tmp(dir)
XPORTA.make_porta_tmp(dir)
ex1_poi_filepath = cp(dir*"example1.poi", dir*"porta_tmp/example1.poi")

# stdout is returned when verbose=false (also returned when true)
return_string = XPORTA.run_xporta("-T", [ex1_poi_filepath], verbose=false)

# setup files for second run
XPORTA.rm_porta_tmp(dir)
XPORTA.make_porta_tmp(dir)
ex1_poi_filepath = cp(dir*"example1.poi", dir*"porta_tmp/example1.poi")

# capturing stdout when verbose=true
capture_string = @capture_out XPORTA.run_xporta("-T", [ex1_poi_filepath], verbose=true)

@test return_string == capture_string

XPORTA.rm_porta_tmp(dir)
end

@testset "test traf (xporta -T) with example1.poi" begin
Expand Down Expand Up @@ -218,4 +240,11 @@ end
end
end

@testset "dim()" begin

# XPORTA.dim(POI(vertices = [1 0 0;0 1 0;0 0 1]), dir=dir, cleanup=false)


end

end

0 comments on commit 5598fb8

Please sign in to comment.