From 6c359a08cde153f53cb735a134d8bedf1a99919b Mon Sep 17 00:00:00 2001 From: OJ Reeves Date: Wed, 3 Feb 2010 21:56:30 +1000 Subject: [PATCH 1/2] Added 3 new templates: - simplefsm - Basic shell of a gen_fsm module - simplesrv - Basic shell of a gen_server module - simplemod - Basic shell of a module along with an associated test module. --- priv/templates/simplefsm.erl | 55 ++++++++++++++++++++++++++++++ priv/templates/simplefsm.template | 2 ++ priv/templates/simplemod.erl | 10 ++++++ priv/templates/simplemod.template | 3 ++ priv/templates/simplemod_tests.erl | 3 ++ priv/templates/simplesrv.erl | 49 ++++++++++++++++++++++++++ priv/templates/simplesrv.template | 2 ++ 7 files changed, 124 insertions(+) create mode 100644 priv/templates/simplefsm.erl create mode 100644 priv/templates/simplefsm.template create mode 100644 priv/templates/simplemod.erl create mode 100644 priv/templates/simplemod.template create mode 100644 priv/templates/simplemod_tests.erl create mode 100644 priv/templates/simplesrv.erl create mode 100644 priv/templates/simplesrv.template diff --git a/priv/templates/simplefsm.erl b/priv/templates/simplefsm.erl new file mode 100644 index 0000000..039532c --- /dev/null +++ b/priv/templates/simplefsm.erl @@ -0,0 +1,55 @@ +-module({{fsmid}}). +-behaviour(gen_fsm). +-define(SERVER, ?MODULE). + +%% ------------------------------------------------------------------ +%% API Function Exports +%% ------------------------------------------------------------------ + +-export([start_link/0]). + +%% ------------------------------------------------------------------ +%% gen_fsm Function Exports +%% ------------------------------------------------------------------ + +-export([init/1, state_name/2, state_name/3, handle_event/3, handle_sync_event/4, handle_info/3, terminate/3, code_change/4]). + +%% ------------------------------------------------------------------ +%% API Function Definitions +%% ------------------------------------------------------------------ + +start_link() -> + gen_fsm:start_link({local, ?SERVER}, ?MODULE, [], []). + +%% ------------------------------------------------------------------ +%% gen_fsm Function Definitions +%% ------------------------------------------------------------------ + +init(_Args) -> + {ok, initial_state_name, initial_state}. + +state_name(_Event, State) -> + {next_state, state_name, State}. + +state_name(_Event, _From, State) -> + {reply, ok, state_name, State}. + +handle_event(_Event, StateName, State) -> + {next_state, StateName, State}. + +handle_sync_event(_Event, _From, StateName, State) -> + {reply, ok, StateName, State}. + +handle_info(_Info, StateName, State) -> + {next_state, StateName, State}. + +terminate(_Reason, _StateName, _State) -> + ok. + +code_change(_OldVsn, StateName, State, _Extra) -> + {ok, StateName, State}. + +%% ------------------------------------------------------------------ +%% Internal Function Definitions +%% ------------------------------------------------------------------ + diff --git a/priv/templates/simplefsm.template b/priv/templates/simplefsm.template new file mode 100644 index 0000000..edb6f56 --- /dev/null +++ b/priv/templates/simplefsm.template @@ -0,0 +1,2 @@ +{variables, [{fsmid, "myfsm"}]}. +{file, "simplefsm.erl", "src/{{fsmid}}.erl"}. diff --git a/priv/templates/simplemod.erl b/priv/templates/simplemod.erl new file mode 100644 index 0000000..2baff59 --- /dev/null +++ b/priv/templates/simplemod.erl @@ -0,0 +1,10 @@ +-module({{modid}}). + +-export([my_func/0]). + +-ifdef(TEST). +-compile(export_all). +-endif. + +my_func() -> + ok. diff --git a/priv/templates/simplemod.template b/priv/templates/simplemod.template new file mode 100644 index 0000000..1de8cd0 --- /dev/null +++ b/priv/templates/simplemod.template @@ -0,0 +1,3 @@ +{variables, [{modid, "mymod"}]}. +{file, "simplemod.erl", "src/{{modid}}.erl"}. +{file, "simplemod_tests.erl", "test/{{modid}}_tests.erl"}. diff --git a/priv/templates/simplemod_tests.erl b/priv/templates/simplemod_tests.erl new file mode 100644 index 0000000..c5ca0bb --- /dev/null +++ b/priv/templates/simplemod_tests.erl @@ -0,0 +1,3 @@ +-module({{modid}}_tests). +-include_lib("eunit/include/eunit.hrl"). + diff --git a/priv/templates/simplesrv.erl b/priv/templates/simplesrv.erl new file mode 100644 index 0000000..8db932e --- /dev/null +++ b/priv/templates/simplesrv.erl @@ -0,0 +1,49 @@ +-module({{srvid}}). +-behaviour(gen_server). +-define(SERVER, ?MODULE). + +%% ------------------------------------------------------------------ +%% API Function Exports +%% ------------------------------------------------------------------ + +-export([start_link/0]). + +%% ------------------------------------------------------------------ +%% gen_server Function Exports +%% ------------------------------------------------------------------ + +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). + +%% ------------------------------------------------------------------ +%% API Function Definitions +%% ------------------------------------------------------------------ + +start_link() -> + gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). + +%% ------------------------------------------------------------------ +%% gen_server Function Definitions +%% ------------------------------------------------------------------ + +init(Args) -> + {ok, Args}. + +handle_call(_Request, _From, State) -> + {noreply, ok, State}. + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info(_Info, State) -> + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +%% ------------------------------------------------------------------ +%% Internal Function Definitions +%% ------------------------------------------------------------------ + diff --git a/priv/templates/simplesrv.template b/priv/templates/simplesrv.template new file mode 100644 index 0000000..e72dd25 --- /dev/null +++ b/priv/templates/simplesrv.template @@ -0,0 +1,2 @@ +{variables, [{srvid, "myserver"}]}. +{file, "simplesrv.erl", "src/{{srvid}}.erl"}. From 71c974e341173da9c5da6f6d6781e2ee52aec251 Mon Sep 17 00:00:00 2001 From: OJ Reeves Date: Wed, 3 Feb 2010 22:27:09 +1000 Subject: [PATCH 2/2] Added support for the speficiation of test suite names eg. rebar eunit -- runs all tests in all modules rebar eunit suite=foo -- only runs tests in foo.erl/foo_tests.erl Added an entry to .hgignore to avoid .swp files (created by VIM). --- .hgignore | 1 + src/rebar_eunit.erl | 39 +++++++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.hgignore b/.hgignore index ad6143a..684e0cb 100644 --- a/.hgignore +++ b/.hgignore @@ -2,3 +2,4 @@ ^rebar$ .~ \.orig +\.swp diff --git a/src/rebar_eunit.erl b/src/rebar_eunit.erl index 6ef7a25..b98a4f9 100644 --- a/src/rebar_eunit.erl +++ b/src/rebar_eunit.erl @@ -51,9 +51,13 @@ eunit(Config, _File) -> %% Make sure ?EUNIT_DIR/ directory exists (tack on dummy module) ok = filelib:ensure_dir(?EUNIT_DIR ++ "/foo"), + %% grab all the test modules for inclusion in the compile stage + TestErls = rebar_utils:find_files("test", ".*\\.erl\$"), + %% Compile erlang code to ?EUNIT_DIR, using a tweaked config - %% with appropriate defines for eunit - rebar_erlc_compiler:doterl_compile(eunit_config(Config), ?EUNIT_DIR), + %% with appropriate defines for eunit, and include all the test modules + %% as well. + rebar_erlc_compiler:doterl_compile(eunit_config(Config), ?EUNIT_DIR, TestErls), %% Build a list of all the .beams in ?EUNIT_DIR -- use this for cover %% and eunit testing. Normally you can just tell cover and/or eunit to @@ -62,8 +66,35 @@ eunit(Config, _File) -> %% we do it by hand. :( %% %% TODO: Not currently compatible with package modules - Modules = [list_to_atom(filename:basename(N, ".beam")) || - N <- filelib:wildcard("*.beam", ?EUNIT_DIR)], + Beams = [filename:basename(N, ".beam") || N <- filelib:wildcard("*.beam", ?EUNIT_DIR)], + + %% Grab two lists of test and non-test beam files + {TestBeams, ModuleBeams} = lists:partition(fun(B) -> + lists:suffix("_tests", B) end, Beams), + + case rebar_config:get_global(suite, undefined) of + undefined -> + %% no suite defined, so include all modules + RealModules = ModuleBeams, + + %% exclude any test modules that have a matching module + TestModules = [T || T <- TestBeams, + lists:member(string:left(T, length(T) - 6), RealModules) == false]; + SuiteName -> + %% suite defined, so only specify the module that relates to the + %% suite (if any) + RealModules = [M || M <- ModuleBeams, SuiteName =:= M], + + %% only include the test suite if the main module doesn't exist + TestModules = case length(RealModules) of + 0 -> [T || T <- TestBeams, T =:= SuiteName ++ "_tests"]; + _ -> [] + end + end, + + %% combine the modules and associated test modules into the resulting list + %% of modules to run tests on. + Modules = [list_to_atom(M) || M <- RealModules ++ TestModules], %% TODO: If there are other wildcards specified in eunit_sources, compile them