Mix projects
A script is one file, and a program grows past one file quickly. mix is the build tool of Elixir: it creates the folder layout, compiles the sources, runs the tests and formats the code. Every project in the rest of the course is a Mix project, and this lesson is the first one from an empty folder.
mix new
mix new hello writes a project named hello with a module, a test and the files that make the build reproducible:
mix new hello
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/hello.ex
* creating test
* creating test/test_helper.exs
* creating test/hello_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd hello
mix test
Run "mix help" for more commands.
The generated test
mix new writes one test and one doctest, and the first mix test compiles the project and runs both. The seed and the time change between runs; the counts do not:
cd hello
mix test
Compiling 1 file (.ex)
Generated hello app
Running ExUnit with seed: 739232, max_cases: 8
..
Finished in 0.08 seconds (0.00s async, 0.08s sync)
1 doctest, 1 test, 0 failures
The generated test file uses ExUnit.Case, pulls the example of the module in with doctest Hello and checks Hello.hello() == :world in one test. The testing lesson takes the file apart; the point here is that the project tests itself from the first minute.
The folder layout
Four names carry the project: mix.exs describes it, lib/ holds the code, test/ holds the tests and _build/ holds what the compiler wrote. The two functions of mix.exs that a reader edits are project/0 and deps/0:
# in mix.exs def project do [ app: :hello, version: "0.1.0", elixir: "~> 1.19", start_permanent: Mix.env() == :prod, deps: deps() ] end defp deps do [] end
The block is the two functions without the surrounding module, which the generator writes and a reader rarely touches. The app and version keys name the application in the runtime, and deps/0 is the list the dependencies lesson fills. The third function of the file, application/0, is the subject of the OTP applications lesson.
The generated module in lib/hello.ex keeps its first function, which the generated test calls, and gains a greeting with an argument. A project compiles every file under lib/, so the module name and the file name do not have to match, but the convention puts Hello.World in lib/hello/world.ex:
hello/lib/hello.ex
defmodule Hello do @moduledoc false @doc """ Hello world. ## Examples iex> Hello.hello() :world """ def hello, do: :world def greeting(name) do "Hello, #{name}!" end end
compile and run
mix compile builds the changed files into _build/dev, and mix run starts the application and evaluates an expression with the project loaded:
mix compile
Compiling 1 file (.ex)
Generated hello app
mix run -e 'IO.puts(Hello.greeting("Ada"))'
Hello, Ada!
A call to mix compile after a change names the number of files it rebuilt, and the second compile with nothing changed prints nothing. mix run without -e starts the application and waits, which is why a script that ends uses -e or an escript.
The formatter file
.formatter.exs lists the files that mix format reads. The generated list covers the project files and the two config files:
# Used by "mix format" [ inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] ]
mix format --check-formatted reads the same list and fails when a file would be rewritten, which is the form a pipeline runs:
mix format --check-formatted
The command printed nothing and exited with 0, which is the pass. The mix format lesson shows the line length and the diff a failing run prints; the testing lesson takes the generated test apart.
The commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Which file describes a Mix project?
Answer
mix.exs
mix.exs holds project/0, application/0 and deps/0, the description of the project.
-
Where does
mix compilewrite the bytecode?Answer
_build/
The build folder holds the compiled beams; the sources stay in lib/.
-
What does
mix new hellocreate?Answer
A project folder with lib, test, mix.exs and a formatter file
The generator writes the layout a project needs to compile and test.
-
What does
mix run -e 'IO.puts(1 + 1)'do?Answer
It starts the application and evaluates the expression
mix run loads the project and evaluates -e in it; -e is the script form.
-
What does this program print?
name = "Ada" IO.puts("Hello, #{name}!")Answer
Hello, Ada!
The greeting interpolates the name and puts the exclamation mark in the string.
-
What does
mix testrun in a generated project?Answer
One test and one doctest
The generated test module has one test and a doctest of the module.
-
What does
mix format --check-formatteddo?Answer
Fails when a file would be rewritten, and prints nothing on success
The check mode reports a file that is not formatted; a clean run exits 0 with no output.
-
Which folder holds the tests of a Mix project?
Answer
test/
The test folder holds the ExUnit files and test_helper.exs.
-
What is the first argument of
mix testoutput that changes between runs?Answer
The seed of the random order
ExUnit prints a seed that changes; the counts and the file names stay.
-
Why does
mix runneed-efor a program that ends?Answer
Without an expression it starts the application and waits
A bare mix run keeps the application alive; -e gives it an expression that ends the run.
Автор статьи: Arthur Isaev