Run terminal command before tests - Elixir

344 Views Asked by At

I'm testing an S3 connection with fakeS3 and it works perfectly. The only downside is I have to run the fake S3 server every time before I run my tests or the S3 test will break obviously.

I want to figure out a way to run this command before my test run:

fakes3 -r ~/.s3bucket -p 4567

This will in theory start the Sinatra server so that the S3 test will pass but I don't need to start a separate server each time.

I've tried this and it didn't work for good reason:

MIX.exs

 test: ["ecto.create --quiet", "ecto.migrate", "fakes3 -r ~/.s3bucket -p 4567", "test"]

That doesn't work because it's not a mix task.

I'm also probably thinking of this the wrong way. How can I have the command run before my tests and is this the right way to think about this problem?

1

There are 1 best solutions below

3
On BEST ANSWER

Just use ExUnit.Callbacks.setup_all/1:

def fakes3(_context) do
  System.cmd("fakes3", ~w|-r ~/.s3bucket -p 4567|)
  :ok
end

setup_all :fakes3

The above is to be put into the case you use to test S3.


BTW, this could be done with mix as well. It has run task that accepts either the script to run or an expression to eval as an argument:

test: [ 
  "ecto.create --quiet",
  "ecto.migrate",
  ~S[run -e "System.cmd(~s|fakes3|, ~w|-r ~/.s3bucket -p 4567|)"],
  "test"
]