Skip to content

Commit

Permalink
Merge d0ed5e0 into 10e2983
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Jun 17, 2016
2 parents 10e2983 + d0ed5e0 commit f18d78b
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 170 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ elixir:
otp_release:
- 18.3
- 18.2
- 18.1
- 18.0
before_script:
- epmd -daemon
script:
- mix cachex.analyze
- mix cachex.coveralls.travis --trace
- mix cachex credo --all --format=oneline
- mix cachex coveralls.travis --trace
branches:
only:
- master
Expand Down
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,11 @@ If you feel something can be improved, or have any questions about certain behav

If you *do* make changes to the codebase, please make sure you test your changes thoroughly, and include any unit tests alongside new or changed behaviours. Cachex currently uses the excellent [excoveralls](https://github.com/parroty/excoveralls) to track code coverage.

```elixir
$ mix cachex.test
$ mix cachex.analyze
$ mix cachex.coveralls
$ mix cachex.coveralls.html && open cover/excoveralls.html
```bash
$ mix cachex test
$ mix cachex credo
$ mix cachex coveralls
$ mix cachex coveralls.html && open cover/excoveralls.html
```

You can replicate the CI build using the task below; this is what's running on Travis CI:

```elixir
$ mix cachex.ci
```
Note that any test-related tasks should be executed under the `cachex` task in order to gain the required context. The first argument is the name of the task to run, and any arguments afterwards are passed as they appear.
56 changes: 29 additions & 27 deletions lib/mix/cachex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,40 @@ defmodule Mix.Cachex do
# `run_task/1` interface which will run the provided function in the scope of
# bound nodes.

# alias internals
alias Cachex.Util
# import utilities
import Cachex.Util

# our list of nodes to create, based on tests
@nodenames [
Util.create_node_name("cachex_test")
create_node_name("cachex_test")
]

@doc """
A small handler to bind any slave nodes to this Mix setup. We first name this
node as Mix doesn't set itself up with a node name. Then we iterate through all
node names and start a `ct_slave`. From that point we ensure that Mnesia is
started on each slave node, and that the Cachex module is available to the node.
Runs a task in a node context.
Internally this just delegates to using `run_task/1` for convenience.
"""
def run_in_context(task, args \\ []) when is_binary(task),
do: run_task(fn -> Mix.Task.run(task, args) end)

@doc """
Runs a task with automatic starting/stopping of any required nodes.
Simply starts all nodes, executes the function, and then closes all nodes.
"""
def run_task(task) when is_function(task) do
start()
task.()
stop()
end

@doc """
Starts a number of slaves nodes against this Mix instance.
We first name this node as Mix doesn't set itself up with a node name. Then we
iterate through all node names and start a `ct_slave`. From that point we ensure
that Mnesia is started on each slave node, and that the Cachex module is available
to the node.
"""
def start do
:net_kernel.start([ :cachex_base_node, :shortnames ])
Expand All @@ -34,29 +55,10 @@ defmodule Mix.Cachex do
end

@doc """
Small handler to stop the slave nodes, simply iterating the names of the nodes
and terminating them.
Stops any slave nodes by iterating the names of the nodes and terminating them.
"""
def stop, do: Enum.each(@nodenames, &stop_node/1)

@doc """
Convenience handler for executing a given function without having to start/stop
any required nodes. Simply starts all nodes, executes the function, and then
closes all nodes.
"""
def run_task(task) when is_function(task) do
start()
task.()
stop()
end

@doc """
Runs a task in a node context. Opens up a node context and runs the given task
name with the given task args. This is just shorthand for convenience.
"""
def run_in_context(task, args) when is_binary(task),
do: run_task(fn -> Mix.Task.run(task, args) end)

# Starts a local node using the :slave module.
defp start_node(longname) do
[ name, host ] =
Expand Down
18 changes: 0 additions & 18 deletions lib/mix/tasks/cachex.analyze.ex

This file was deleted.

22 changes: 0 additions & 22 deletions lib/mix/tasks/cachex.ci.ex

This file was deleted.

69 changes: 0 additions & 69 deletions lib/mix/tasks/cachex.coveralls.ex

This file was deleted.

49 changes: 49 additions & 0 deletions lib/mix/tasks/cachex.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
defmodule Mix.Tasks.Cachex do
use Mix.Task

@moduledoc false
# A neat Mix Task to automate the wrapping of tasks in a Cachex context. A context
# will start any Cachex required test nodes so that they're available for testing
# replication and remote features.
#
# The first argument to this task must be the name of the task you wish to run.
# Any arguments thereafter will be passed directly to that task. This allows
# you to easily start a test run by doing something like this:
#
# mix cachex test --trace
#
# This task exists to make it flexible to run Cachex test-based tools, whereas
# previously we defined a task per those we recognised, which naturally didn't
# scale particularly well.

# import context tools
import Mix.Cachex

@doc """
Provides access to tasks inside a Cachex context.
This is required as there are several nodes to stop/start for things such as
test runs. Running via this Task will start the context, run the provided task,
and then close the context.
The first argument must always be a task to execute, and any other arguments
as passed directly through to the Mix Task itself.
As an example:
Mix.Tasks.Cachex.run([ "test", "--trace" ])
will execute the `test` task, with the argument `--trace`.
"""
@spec run(OptionParser.argv) :: no_return
def run([task]) do
run_in_context(task)
end
def run([task | args]) do
run_in_context(task, args)
end
def run([]) do
Mix.raise("A task name must be provided as first argument")
end

end
18 changes: 0 additions & 18 deletions lib/mix/tasks/cachex.test.ex

This file was deleted.

3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ defmodule Cachex.Mixfile do
tool: ExCoveralls
],
preferred_cli_env: [
"cachex.ci": :test,
"cachex.test": :test
"cachex": :test
]
]
end
Expand Down

0 comments on commit f18d78b

Please sign in to comment.