ReTest, module system issues

Hello, good afternoon,
I’m trying to test some modules, but I’m having issues with ReTest and the module system. I had this custom modules and is all added to “.” path. RunTests.jl file, I’m getting the

UndefVarError: float_equality not defined

cant understand why

below the source code:

file Primitives.jl

module Primitives
using Base: +

const EPSILON = 0.0001

function float_equality(a::Float64, b::Float64)::Bool
    if abs(a - b) < EPSILON
        return true
    else
        return false
    end
end

struct Vec3D
    x::Float64
    y::Float64
    z::Float64
end

struct Point3D
    x::Float64
    y::Float64
    z::Float64
end
end

file PrimitivesTest.jl

module PrimitivesTest

include("Primitives.jl")

using .Primitives, ReTest

@testset "float_equality should return true" begin

    @test float_equality(0.1, 0.1222)

end
end

file RunTests.jl

include("PrimitivesTest.jl")
include("Primitives.jl")
using ReTest
retest(Primitives, PrimitivesTest) # error goes here

You do not explicitly export or import float_equality. This your primary issue at the moment.

Also in runtests.jl you should probably not be using include. Instead use using or import.

I recommend closely reading the documentation below. There seems to be significant confusion on the use of include and using along with a lack of import and export here.

https://docs.julialang.org/en/v1/manual/code-loading/

Yes. I was in doubt about

using .Primitives

or

import .Primitives

but Im already using “using”. There must be a way to add the content in current file first.

Neither worked without import

You need import .Primitives: float_equality.

module PrimitivesTest

include("Primitives.jl")

using .Primitives, ReTest
import .Primitives: float_equality

@testset "float_equality should return true" begin

    @test float_equality(0.1, 0.1222)

end
end

Alternatively, you need to export it.

module Primitives
using Base: +

# float_equality will be now be imported automatically when you do `using Primitives`
export float_equality 

const EPSILON = 0.0001

function float_equality(a::Float64, b::Float64)::Bool
    if abs(a - b) < EPSILON
        return true
    else
        return false
    end
end
...
end

I would be interested in understand your file tree structure and how you run tests. Based on my current understanding, this is how I would arrange your project.

Primitives/
   Project.toml
   Manifest.toml
   src/Primitives.jl
   test/runtests.jl
   test/PrimitivesTest.jl

Primitives.jl:

module Primitives
using Base: +

export float_equality

const EPSILON = 0.0001

function float_equality(a::Float64, b::Float64)::Bool
    if abs(a - b) < EPSILON
        return true
    else
        return false
    end
end

struct Vec3D
    x::Float64
    y::Float64
    z::Float64
end

struct Point3D
    x::Float64
    y::Float64
    z::Float64
end

PrimitivesTest.jl

module PrimitivesTest

using Primitives, ReTest

@testset "float_equality should return true" begin

    @test float_equality(0.1, 0.1222)

end
end

runtests.jl:

using Primitives, ReTest
include("PrimitivesTest.jl")
using .PrimitivesTest

retest(Primitives, PrimitivesTest)

If the current directory were Primitives, then I would run julia --project=. -e "using Pkg; Pkg.test()".

Not worked

You need to name your project:

https://pkgdocs.julialang.org/v1/toml-files/#Project.toml

Here is an example:

The usual way to create this is to use Pkg.generate("PackageName") or use PkgTemplates.jl.

julia> using Pkg

help?> Pkg.generate
  Pkg.generate(pkgname::String)

  Create a minimal project called pkgname in the current folder. For more featureful package creation, please
  see PkgTemplates.jl.
1 Like

If you need to generate a uuid by hand, try this:

julia> using UUIDs

julia> uuid4()
UUID("07a100d5-36e3-4009-9ea3-ad17279780ff")

there are a way to run these tests in a simple repl with only an simple activated workspace? There are a lot of issues. Just test it in vscode or rpl for simplicity.

From the REPL, I would do

] activate .
] test

where the current directory is Primitives or perhaps rtc in your case.

I’ll need to build a full project from scratch using PakageTemplates.jl… just activate it just not worked.
I hadnt the filesystem strucure for a project. Using julia ] activate . only give me files in root folder

image

its different now when I tryed julia some year ago. Worst is, all book I have about julia is obsolete :neutral_face:

What is your current file structure?

Since you are not working out of .julia/dev/ you might need to work on your LOAD_PATH. Also check the capitalization of test/runtests.jl. It should be lower case.

If it’s pre-1.0 (August 8th, 2018), then it’s probably obsolete. There is a book list here:

I’m rebuilding all my project.

Only using “using” did not find the modules

The problem is that the directory and project is named “rtc”. If you did “using rtc” it would know where to find the rtc module.

What is the content of rtc.jl?

One way would be to do this as follows:

rtc.jl:

module rtc
    include("Primitives.jl")
    ...
end

PrimitivesTest.jl:

module PrimitivesTest

using  rtc, rtc.Primitives, ReTest

@testset "float_equality should return true" begin

    @test float_equality(0.1, 0.1222)

end
end

if I use Pkg.generate(“rtc”) the folder will aways be called rtc. Tryed the way you did also, and the Primitives cant be found yet.

Right. When you Pkg.generate("rtc"), it is expecting a module called rtc. With your current layout, Primitives is now a submodule of rtc, so we have load rtc and then load rtc.Primitives. Can it not find rtc.Primitives?