lamtrhieu

lamtrhieu

Hi everyone,
I have a module with several public api and the implementation for these public require call to several private functions. Something like this

def foo do
  bar 
  baa 
  baz
end

defp baa do
...
end
defp baz do 
...
end
defp bar do
.....
end

And baa have 2 cases to test, baz have 3 cases, and bar have 5 cases. So if I just keep them as private functions inside 1 module, I need to write 2x3x5=30 unit tests.
So I refactor them to separate module and make them public so I just need to write 2+3+5=10 unit tests and some smoke tests for foo which don’t cover all the scenarios.

Is that approach ok and how I prevent users from misused my private module ? I just want the user to use my public module only.

Hope to hear your thoughts on this matter. Thanks

Showing Posts 1 to 9

NobbZ

NobbZ

The convention is that internal modules are @moduledoc false, similar how internal functions are @doc false, though you have no way to enforce that no one will use them.

There is some prototype library defmodulep which hides modules by name doing some name mangling, but still, once someone figured out the mangled name, they can use the module anyway.

chrismcg

chrismcg

I wonder if boundaries will end up being the way to solve this in future. Make the functions public but enforce that only the parent module can call them outside of tests.

NobbZ

NobbZ

There is no way of really enforcing that in the BEAM, as there are no parent/child relationships between modules. All of those tools rely on mangling and aliasing module names.

And if you know the mangled name, you can use anything in that module.

sasajuric

sasajuric

Author of Elixir In Action

It can already solve it today :slight_smile: Add use Boundary to the top-level module (e.g. context), and the cross-boundary calls to the internal ones will be reported. It’s still not fully usable, b/c the support for nested boundaries is lacking, but that’s definitely on the roadmap.

Boundary doesn’t do any mangling. It uses compilation tracer to collect all function/macro invocations, and then reports non-permitted cross-boundary calls.

It’s definitely not bulletproof. Breaking it is currently as easy as using apply to dynamically invoke the function. This can be detected though, and I plan to address it, but even then you can work around by constructing the module name dynamically.

All that said, boundary will make you work harder to introduce unwanted dependencies into your codebase.

hauleth

hauleth

I have created ExUnitEmbedded for such purposes. It is idea similar to EUnit tests within modules themselves.

chrismcg

chrismcg

It would certainly help stop “accidental” usage where someone who doesn’t understand the @moduledoc false idiom calls a function directly instead of through private api.

chrismcg

chrismcg

Interesting! I have like that style of testing when I’ve done it in Rust

lamtrhieu

lamtrhieu OP

Thanks all for the information and suggestion. For now, I think I will just apply @moduledoc false and @doc false as an indicator that a module or function is private and should not be called directly.
Other solution like defmodulep, boundary seems a lot heavy weight and add more load to developers. I like the idea but apply to my real project seems not feasible.
unitembdded is the most promising but I don’t like the idea of writing module code and tests together.
So I would go with the convention approach and developer should be responsible when use a module/function

riebeekn

riebeekn

You might also find something like https://github.com/TeachersPayTeachers/publicist or https://github.com/pragdave/private useful if you’re just looking to be able to test your private functions… the above packages allow you to expose private functions as public when under test.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews