Skip to content

Commit

Permalink
Making it an error to reduce over an empty collection of AbstractCmd …
Browse files Browse the repository at this point in the history
…with & operator

Also adding equality operator for AndCmds to facilitate unit testing.
  • Loading branch information
JohnHBrock committed May 22, 2016
1 parent 6a837da commit 2a63b3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 5 additions & 0 deletions base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ immutable AndCmds <: AbstractCmd
AndCmds(a::AbstractCmd, b::AbstractCmd) = new(a, b)
end

hash(x::AndCmds, h::UInt) = hash(x.a, hash(x.b, h))
==(x::AndCmds, y::AndCmds) = x.a == y.a && x.b == y.b

shell_escape(cmd::Cmd) = shell_escape(cmd.exec...)

function show(io::IO, cmd::Cmd)
Expand Down Expand Up @@ -235,6 +238,8 @@ setenv(cmd::Cmd; dir="") = Cmd(cmd; dir=dir)
(&)(left::AbstractCmd, right::AbstractCmd) = AndCmds(left, right)
redir_out(src::AbstractCmd, dest::AbstractCmd) = OrCmds(src, dest)
redir_err(src::AbstractCmd, dest::AbstractCmd) = ErrOrCmds(src, dest)
Base.mr_empty{T2<:Base.AbstractCmd}(f, op::typeof(&), T1::Type{T2}) =
throw(ArgumentError("reducing over an empty collection of type $T1 with operator & is not allowed"))

# Stream Redirects
redir_out(dest::Redirectable, src::AbstractCmd) = CmdRedirect(src, dest, STDIN_NO)
Expand Down
10 changes: 9 additions & 1 deletion test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ end
@windows_only ENV["PATH"] = oldpath

# equality tests for Cmd

@test Base.Cmd(``) == Base.Cmd(``)
@test Base.Cmd(`lsof -i :9090`) == Base.Cmd(`lsof -i :9090`)
@test Base.Cmd(`echo test`) == Base.Cmd(`echo test`)
Expand All @@ -354,3 +353,12 @@ end
@test Base.Set([``, ``]) == Base.Set([``])
@test Set([``, `echo`]) != Set([``, ``])
@test Set([`echo`, ``, ``, `echo`]) == Set([`echo`, ``])

# equality tests for AndCmds
@test Base.AndCmds(`echo abc`, `echo def`) == Base.AndCmds(`echo abc`, `echo def`)
@test Base.AndCmds(`echo abc`, `echo def`) != Base.AndCmds(`echo abc`, `echo xyz`)

# tests for reducing over collection of Cmd
@test_throws ArgumentError reduce(&, Base.AbstractCmd[])
@test_throws ArgumentError reduce(&, Base.Cmd[])
@test reduce(&, [`echo abc`, `echo def`, `echo hij`]) == `echo abc` & `echo def` & `echo hij`

0 comments on commit 2a63b3a

Please sign in to comment.