From 3db8f585f11eb7d7a9573d9f3a5480f6a900e6d7 Mon Sep 17 00:00:00 2001 From: Tim Watson Date: Wed, 26 Jan 2011 02:00:53 +0000 Subject: [PATCH] Add -spec support to rebar_ct command line This change adds support for executing ct test runs based on test specificiations, which was missing previously. The rebar_ct module now looks for any number of files with a name ending in `test.spec` and if it finds one or more, passes these after the `-spec` argument to ct_run instead of explicitly configuring the config, user config and coverage config variables. When no specifications are found, then the module behaves as it did before this change, and both the ct1 and (new) ct2 integration tests appear to show this is a backwards compatible patch. --- include/rebar.hrl | 1 - inttest/ct2/ct2_rt.erl | 26 +++++++++++++++ inttest/ct2/foo.test.spec | 1 + inttest/ct2/foo_SUITE.erl | 10 ++++++ src/rebar_ct.erl | 70 +++++++++++++++++++++++++++------------ 5 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 inttest/ct2/ct2_rt.erl create mode 100644 inttest/ct2/foo.test.spec create mode 100644 inttest/ct2/foo_SUITE.erl diff --git a/include/rebar.hrl b/include/rebar.hrl index d5eaff5..66d6318 100644 --- a/include/rebar.hrl +++ b/include/rebar.hrl @@ -15,4 +15,3 @@ -define(ERROR(Str, Args), rebar_log:log(error, Str, Args)). -define(FMT(Str, Args), lists:flatten(io_lib:format(Str, Args))). - diff --git a/inttest/ct2/ct2_rt.erl b/inttest/ct2/ct2_rt.erl new file mode 100644 index 0000000..2b14ff9 --- /dev/null +++ b/inttest/ct2/ct2_rt.erl @@ -0,0 +1,26 @@ +-module(ct2_rt). + +-compile(export_all). + + +files() -> + [{create, "ebin/foo.app", app(foo)}, + {copy, "../../rebar", "rebar"}, + {copy, "foo.test.spec", "test/foo.test.spec"}, + {copy, "foo_SUITE.erl", "test/foo_SUITE.erl"}]. + +run(_Dir) -> + {ok, _} = retest:sh("./rebar compile ct -v"), + ok. + +%% +%% Generate the contents of a simple .app file +%% +app(Name) -> + App = {application, Name, + [{description, atom_to_list(Name)}, + {vsn, "1"}, + {modules, []}, + {registered, []}, + {applications, [kernel, stdlib]}]}, + io_lib:format("~p.\n", [App]). diff --git a/inttest/ct2/foo.test.spec b/inttest/ct2/foo.test.spec new file mode 100644 index 0000000..f3e4cb0 --- /dev/null +++ b/inttest/ct2/foo.test.spec @@ -0,0 +1 @@ +{suites, "test", all}. diff --git a/inttest/ct2/foo_SUITE.erl b/inttest/ct2/foo_SUITE.erl new file mode 100644 index 0000000..d03aedf --- /dev/null +++ b/inttest/ct2/foo_SUITE.erl @@ -0,0 +1,10 @@ +-module(foo_SUITE). + +-include_lib("common_test/include/ct.hrl"). + +-compile(export_all). + +all() -> [foo]. + +foo(Config) -> + io:format("Test: ~p\n", [Config]). diff --git a/src/rebar_ct.erl b/src/rebar_ct.erl index 8f7c71a..a66bdce 100644 --- a/src/rebar_ct.erl +++ b/src/rebar_ct.erl @@ -141,32 +141,57 @@ make_cmd(TestDir, Config) -> CodeDirs = [io_lib:format("\"~s\"", [Dir]) || Dir <- [EbinDir|NonLibCodeDirs]], CodePathString = string:join(CodeDirs, " "), - Cmd = ?FMT("erl " % should we expand ERL_PATH? - " -noshell -pa ~s ~s" - " -s ct_run script_start -s erlang halt" - " -name test@~s" - " -logdir \"~s\"" - " -env TEST_DIR \"~s\"", - [CodePathString, - Include, - net_adm:localhost(), - LogDir, - filename:join(Cwd, TestDir)]) ++ - get_cover_config(Config, Cwd) ++ - get_ct_config_file(TestDir) ++ - get_config_file(TestDir) ++ - get_suite(TestDir) ++ - get_case(), + Cmd = case get_ct_specs(Cwd) of + undefined -> + ?FMT("erl " % should we expand ERL_PATH? + " -noshell -pa ~s ~s" + " -s ct_run script_start -s erlang halt" + " -name test@~s" + " -logdir \"~s\"" + " -env TEST_DIR \"~s\"", + [CodePathString, + Include, + net_adm:localhost(), + LogDir, + filename:join(Cwd, TestDir)]) ++ + get_cover_config(Config, Cwd) ++ + get_ct_config_file(TestDir) ++ + get_config_file(TestDir) ++ + get_suite(TestDir) ++ + get_case(); + SpecFlags -> + ?FMT("erl " % should we expand ERL_PATH? + " -noshell -pa ~s ~s" + " -s ct_run script_start -s erlang halt" + " -name test@~s" + " -logdir \"~s\"" + " -env TEST_DIR \"~s\"", + [CodePathString, + Include, + net_adm:localhost(), + LogDir, + filename:join(Cwd, TestDir)]) ++ + SpecFlags ++ get_cover_config(Config, Cwd) + end, RawLog = filename:join(LogDir, "raw.log"), {Cmd, RawLog}. +get_ct_specs(Cwd) -> + case collect_glob(Cwd, ".*\.test\.spec\$") of + [] -> undefined; + [Spec] -> + " -spec " ++ Spec; + Specs -> + " -spec " ++ + lists:flatten([io_lib:format("~s ", [Spec]) || Spec <- Specs]) + end. + get_cover_config(Config, Cwd) -> case rebar_config:get_local(Config, cover_enabled, false) of false -> ""; true -> - case filelib:fold_files(Cwd, ".*cover\.spec\$", - true, fun collect_ct_specs/2, []) of + case collect_glob(Cwd, ".*cover\.spec\$") of [] -> ?DEBUG("No cover spec found: ~s~n", [Cwd]), ""; @@ -178,15 +203,18 @@ get_cover_config(Config, Cwd) -> end end. -collect_ct_specs(F, Acc) -> +collect_glob(Cwd, Glob) -> + filelib:fold_files(Cwd, Glob, true, fun collect_files/2, []). + +collect_files(F, Acc) -> %% Ignore any specs under the deps/ directory. Do this pulling %% the dirname off the the F and then splitting it into a list. Parts = filename:split(filename:dirname(F)), case lists:member("deps", Parts) of true -> - Acc; % There is a directory named "deps" in path + Acc; % There is a directory named "deps" in path false -> - [F | Acc] % No "deps" directory in path + [F | Acc] % No "deps" directory in path end. get_ct_config_file(TestDir) ->