Repository: JuliaLang/julia
File: test/cmdlineargs.jl (Julia)

Code before:
        catch ex
            (ex isa Base.IOError && ex.code == Base.UV_ENOENT) || rethrow(ex)
            ""
        end
    end
    @test startswith(fn, "a$hd ")
    @test endswith(fn, " z")
    @test !occursin('%', fn)
    @test occursin(" $(getpid()) ", fn)
    @test occursin(" $(Libc.gethostname()) ", fn)
    @test format_filename("%a%%b") == "a%b"
end

if Sys.isunix()
    @testset "SIGQUIT prints task backtraces" begin
        script = """
            mutable struct RLimit
                cur::Int64
                max::Int64
            end
            const RLIMIT_CORE = 4 # from /usr/include/sys/resource.h
            ccall(:setrlimit, Cint, (Cint, Ref{RLimit}), RLIMIT_CORE, Ref(RLimit(0, 0)))
            sleep(20.0) # allow Julia to start
        """
        exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
        errp = Base.PipeEndpoint()
        # disable coredumps for this process
        p = run(`$exename -e $script`, devnull, devnull, errp, wait=false)
        sleep(5.0) # allow Julia to start
        Base.kill(p, Base.SIGQUIT)
        wait(p)
        err_s = readchomp(errp)
        @test Base.process_signaled(p) && p.termsignal == Base.SIGQUIT
        @test occursin("==== Thread ", err_s)
        @test occursin("==== Done", err_s)
    end
end

@testset "julia_cmd" begin
    julia_basic = Base.julia_cmd()
    function get_julia_cmd(arg)
        io = Base.BufferStream()
        cmd = `$julia_basic $arg -e 'print(repr(Base.julia_cmd()))'`
        try
            run(pipeline(cmd, stdout=io, stderr=io))
        catch
            closewrite(io)
            @error "cmd failed" cmd read(io, String)
            rethrow()
        end
        closewrite(io)

Review comment (bug):
```suggestion
        errp = IOBuffer()
```
(this was previously a data race, which made this test dangerous)

Code after:
        catch ex
            (ex isa Base.IOError && ex.code == Base.UV_ENOENT) || rethrow(ex)
            ""
        end
    end
    @test startswith(fn, "a$hd ")
    @test endswith(fn, " z")
    @test !occursin('%', fn)
    @test occursin(" $(getpid()) ", fn)
    @test occursin(" $(Libc.gethostname()) ", fn)
    @test format_filename("%a%%b") == "a%b"
end

if Sys.isunix()
    @testset "SIGQUIT prints task backtraces" begin
        script = """
            mutable struct RLimit
                cur::Int64
                max::Int64
            end
            const RLIMIT_CORE = 4 # from /usr/include/sys/resource.h
            ccall(:setrlimit, Cint, (Cint, Ref{RLimit}), RLIMIT_CORE, Ref(RLimit(0, 0)))
            write(stdout, "r")
            wait()
        """
        exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
        errp = PipeBuffer()
        # disable coredumps for this process
        p = open(pipeline(`$exename -e $script`, stderr=errp), "r")
        @test read(p, UInt8) == UInt8('r')
        Base.kill(p, Base.SIGQUIT)
        wait(p)
        err_s = readchomp(errp)
        @test Base.process_signaled(p) && p.termsignal == Base.SIGQUIT
        @test occursin("==== Thread ", err_s)
        @test occursin("==== Done", err_s)
    end
end

@testset "julia_cmd" begin
    julia_basic = Base.julia_cmd()
    function get_julia_cmd(arg)
        io = Base.BufferStream()
        cmd = `$julia_basic $arg -e 'print(repr(Base.julia_cmd()))'`
        try
            run(pipeline(cmd, stdout=io, stderr=io))
        catch
            closewrite(io)
            @error "cmd failed" cmd read(io, String)
            rethrow()
        end