<NME> tests.jl
<BEF> # Tests for doctest = :fix
#
# DOCUMENTER_TEST_DEBUG= JULIA_DEBUG=all julia test/doctests/fix/tests.jl
#
module DocTestFixTest
using Documenter, Test
include("../../TestUtilities.jl"); using Main.TestUtilities: @quietly

# Type to reliably show() objects across Julia versions:
@eval Main begin
    struct ShowWrap
        s :: String
    end
    Base.show(io::IO, x::ShowWrap) = write(io, x.s)
    const DocTestFixArray_1234 = Main.ShowWrap("4×1×1 Array{Int64,3}:\n[:, :, 1] =\n 1\n 2\n 3\n 4")
    const DocTestFixArray_2468 = Main.ShowWrap("4×1×1 Array{Int64,3}:\n[:, :, 1] =\n 2\n 4\n 6\n 8")
end

mktempdir_nocleanup(dir) = mktempdir(dir, cleanup = false)
# The version check is necessary due to a behaviour change in https://github.com/JuliaLang/julia/pull/32851
mktempdir_nocleanup(dir) = VERSION >= v"1.3.0-alpha.112" ? mktempdir(dir, cleanup = false) : mktempdir(dir)

function test_doctest_fix(dir)
    srcdir = mktempdir_nocleanup(dir)
    builddir = mktempdir_nocleanup(dir)
    # Pkg.add changes permission of files to read-only,

    # Pkg.add changes permission of files to read-only,
    # so instead of copying them we read + write.
    write(joinpath(srcdir, "index.md"), read(joinpath(@__DIR__, "broken.md")))
    write(joinpath(srcdir, "src.jl"), read(joinpath(@__DIR__, "broken.jl")))

    # fix up
    include(joinpath(srcdir, "src.jl")); @eval import .Foo

    # test that strict = true works
    include(joinpath(srcdir, "src.jl")); @eval import .Foo
    @debug "Running doctest/fix doctests with doctest=true"
    @quietly makedocs(sitename="-", modules = [Foo], source = srcdir, build = builddir, strict = true)

    @quietly makedocs(sitename="-", modules = [Foo], source = srcdir, build = builddir, strict = true)

    # also test that we obtain the expected output
    @test read(joinpath(srcdir, "index.md"), String) == read(joinpath(@__DIR__, "fixed.md"), String)
    @test read(joinpath(srcdir, "src.jl"), String) == read(joinpath(@__DIR__, "fixed.jl"), String)
end

@testset "doctest fixing" begin
        test_doctest_fix(mktempdir_nocleanup(@__DIR__))
    else
        mktempdir(test_doctest_fix, @__DIR__)
    end
end

end # module

<MSG> Fix cross-platform newline handling in doctests (#1520)

Co-authored-by: Oscar Dowson <0bb614df12a63a596479e422c9064f4d6232e9cf@users.noreply.github.com>
<DFF> @@ -20,6 +20,11 @@ end
 # The version check is necessary due to a behaviour change in https://github.com/JuliaLang/julia/pull/32851
 mktempdir_nocleanup(dir) = VERSION >= v"1.3.0-alpha.112" ? mktempdir(dir, cleanup = false) : mktempdir(dir)
 
+function normalize_line_endings(filename)
+    s = read(filename, String)
+    return replace(s, "\r\n" => "\n")
+end
+
 function test_doctest_fix(dir)
     srcdir = mktempdir_nocleanup(dir)
     builddir = mktempdir_nocleanup(dir)
@@ -27,8 +32,10 @@ function test_doctest_fix(dir)
 
     # Pkg.add changes permission of files to read-only,
     # so instead of copying them we read + write.
-    write(joinpath(srcdir, "index.md"), read(joinpath(@__DIR__, "broken.md")))
-    write(joinpath(srcdir, "src.jl"), read(joinpath(@__DIR__, "broken.jl")))
+    src_jl = joinpath(srcdir, "src.jl")
+    index_md = joinpath(srcdir, "index.md")
+    write(index_md, normalize_line_endings(joinpath(@__DIR__, "broken.md")))
+    write(src_jl, normalize_line_endings(joinpath(@__DIR__, "broken.jl")))
 
     # fix up
     include(joinpath(srcdir, "src.jl")); @eval import .Foo
@@ -41,8 +48,8 @@ function test_doctest_fix(dir)
     @quietly makedocs(sitename="-", modules = [Foo], source = srcdir, build = builddir, strict = true)
 
     # also test that we obtain the expected output
-    @test read(joinpath(srcdir, "index.md"), String) == read(joinpath(@__DIR__, "fixed.md"), String)
-    @test read(joinpath(srcdir, "src.jl"), String) == read(joinpath(@__DIR__, "fixed.jl"), String)
+    @test normalize_line_endings(index_md) == normalize_line_endings(joinpath(@__DIR__, "fixed.md"))
+    @test normalize_line_endings(src_jl) == normalize_line_endings(joinpath(@__DIR__, "fixed.jl"))
 end
 
 @testset "doctest fixing" begin

Fix cross-platform newline handling in doctests (#1520)