How to make `mix test` work in umbrella root and inside child app?

Background

I have an umbrella project with several apps. One of its child applications needs to read a file before it starts. If it cannot find the file, it crashes.

This means that if I want to run mix test, the child application has to find said file.

Problem

The problem here is that depending where I run mix test, the command will either fail or succeed.

If I run mix test inside the child’s application folder, the command will fail because the file it needs is not there.

If I runt he mix test at the root of my umbrella app, then the command succeeds because it can find said file.

This means that I am cannot run mix test while inside the child application folder, which is quite cumbersome. Sometimes I will open only that 1 application and do some changes there, other times I open the full umbrella project and make changes in multiple places.

Question

Is it possible to have mix test work both at the root of the umbrella project and inside its child app ?

This is the folder structure I have:

β”œβ”€β”€β”€config
|     β”œβ”€β”€β”€prod.exs
|     β”œβ”€β”€β”€dev.exs
|     └───test.exs
β”œβ”€β”€β”€assets
|    └─── products.json
β”œβ”€β”€β”€apps
β”‚   β”œβ”€β”€β”€manager
|   └─── ... 
β”‚   β”œβ”€β”€β”€store
β”‚   β”‚   β”œβ”€β”€β”€lib
β”‚   β”‚   β”‚   └───store
β”‚   β”‚   └───test
β”‚   β”‚       └───...

I have the following in my config/test.exs (root folder of umbrella project):

import Config

config :store,
  products: "assets/products.json",

I guess I could simply duplicate the products.json file everywhere in every app that needs to run a test using products, but I find this duplication rather unwise.

What is the preferred solution to this?

Use an absolute path or a relative one, which is relative to some well known location and not relative to your working directory. E.g. consider using __DIR__ to use a relative path to where the config file lives instead of having it be relative to the cwd.

1 Like

Would you then recommend I change the config/test.exs to something like this?

config :store,
  products: "__DIR__ <> assets/products.json",

The problem here is that config/test.exs is inside the config folder, so __DIR__ will return "my_app/config". This is a problem, since the folder I want to access (assets) is at the same level of config.

I tried using Path.wildcard but I think I am not using it properly:

 Path.wildcard("#{__DIR__}/../assets/products.json")

> PATH IS: ["c:/Users/my_app/config/../assets/products.json"]

As mix test still complains the files are missing

Correction: I should be using Path.expand (Path β€” Elixir v1.13.4) instead, which will result in:

Path.expand("#{__DIR__}/../assets/products.json")

>  PATH IS: ["c:/Users/my_app/assets/products.json"]