Merge pull request #588 from lrascao/fix/look_for_ct_spec_files_only_in_test_dir

Look for ct .spec files in the ct_dir that was specified
diff --git a/inttest/ct4/ct4_rt.erl b/inttest/ct4/ct4_rt.erl
new file mode 100644
index 0000000..bc010e8
--- /dev/null
+++ b/inttest/ct4/ct4_rt.erl
@@ -0,0 +1,44 @@
+%% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
+%% ex: ts=4 sw=4 et
+-module(ct4_rt).
+
+-compile(export_all).
+
+setup([Target]) ->
+  retest_utils:load_module(filename:join(Target, "inttest_utils.erl")),
+  ok.
+
+files() ->
+    [
+     {create, "ebin/foo.app", app(foo)},
+     {copy, "rebar.config", "rebar.config"},
+     {copy, "foo.test.spec", "test/foo.test.spec"},
+     {copy, "deps/bar.test.spec", "deps/bar.test.spec"},
+     {copy, "deps/bar.test.spec", "baz.test.spec"},
+     {copy, "foo_SUITE.erl", "test/foo_SUITE.erl"}
+    ] ++ inttest_utils:rebar_setup().
+
+run(_Dir) ->
+    Ref = retest:sh("./rebar compile ct -vvv", [async]),
+    {ok, [[CTRunCmd]]} = retest:sh_expect(Ref, "^\"ct_run.*",
+                                  [global, {capture, first, binary}]),
+    {match, _} = re:run(CTRunCmd, "foo.test.spec", [global]),
+    %% deps/bar.test.spec should be ignored by rebar_ct:collect_glob/3
+    nomatch = re:run(CTRunCmd, "bar.test.spec", [global]),
+    %% baz.test.spec should be also ignored by rebar_ct:collect_glob/3
+    %% since we specified in rebar.config that we want to search for
+    %% ct specs from the test dir
+    nomatch = re:run(CTRunCmd, "baz.test.spec", [global]),
+    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/ct4/deps/bar.test.spec b/inttest/ct4/deps/bar.test.spec
new file mode 100644
index 0000000..a16610c
--- /dev/null
+++ b/inttest/ct4/deps/bar.test.spec
@@ -0,0 +1 @@
+%% this test spec should be ignored
diff --git a/inttest/ct4/foo.test.spec b/inttest/ct4/foo.test.spec
new file mode 100644
index 0000000..1850410
--- /dev/null
+++ b/inttest/ct4/foo.test.spec
@@ -0,0 +1 @@
+{suites, "../", [foo_SUITE]}.
diff --git a/inttest/ct4/foo_SUITE.erl b/inttest/ct4/foo_SUITE.erl
new file mode 100644
index 0000000..fb4f56a
--- /dev/null
+++ b/inttest/ct4/foo_SUITE.erl
@@ -0,0 +1,11 @@
+-module(foo_SUITE).
+
+-include_lib("common_test/include/ct.hrl").
+
+-compile(export_all).
+
+all() -> [simple].
+
+simple(Config) ->
+    io:format("Test: ~p\n", [Config]),
+    ok.
diff --git a/inttest/ct4/rebar.config b/inttest/ct4/rebar.config
new file mode 100644
index 0000000..387f8c3
--- /dev/null
+++ b/inttest/ct4/rebar.config
@@ -0,0 +1,2 @@
+{ct_dir, "test"}.
+{ct_search_specs_from_test_dir, true}.
diff --git a/rebar.config.sample b/rebar.config.sample
index 916e47c..224c85c 100644
--- a/rebar.config.sample
+++ b/rebar.config.sample
@@ -148,6 +148,10 @@
 %% Option to use short names (i.e., -sname test) when starting ct
 {ct_use_short_names, true}.
 
+%% Recursively search for .spec files from the test dir, default
+%% is false (ie. the search will be from the current working directory)
+{ct_search_specs_from_test_dir, true}.
+
 %% == QuickCheck ==
 
 %% If qc_mod is unspecified, rebar tries to detect Triq or EQC
diff --git a/src/rebar_ct.erl b/src/rebar_ct.erl
index cf2059e..b27f661 100644
--- a/src/rebar_ct.erl
+++ b/src/rebar_ct.erl
@@ -66,6 +66,7 @@
        "  ~p~n"
        "  ~p~n"
        "  ~p~n"
+       "  ~p~n"
        "Valid command line options:~n"
        "  suites=Suite1,Suite2,...,SuiteN~n"
        "      - run Suite1_SUITE, Suite2_SUITE, ..., SuiteN_SUITE~n"
@@ -81,7 +82,8 @@
         {ct_dir, "itest"},
         {ct_log_dir, "test/logs"},
         {ct_extra_params, "-boot start_sasl -s myapp"},
-        {ct_use_short_names, true}
+        {ct_use_short_names, true},
+        {ct_search_specs_from_test_dir, false}
        ]).
 
 run_test_if_present(TestDir, LogDir, Config, File) ->
@@ -235,7 +237,7 @@
     CodeDirs = [io_lib:format("\"~s\"", [Dir]) ||
                    Dir <- [EbinDir|NonLibCodeDirs]],
     CodePathString = string:join(CodeDirs, " "),
-    Cmd = case get_ct_specs(Config, Cwd) of
+    Cmd = case get_ct_specs(Config, search_ct_specs_from(Cwd, TestDir, Config)) of
               undefined ->
                   ?FMT("~s"
                        " -pa ~s"
@@ -276,10 +278,20 @@
     RawLog = filename:join(LogDir, "raw.log"),
     {Cmd, RawLog}.
 
+search_ct_specs_from(Cwd, TestDir, Config) ->
+    case rebar_config:get_local(Config, ct_search_specs_from_test_dir, false) of
+        true -> filename:join(Cwd, TestDir);
+        false ->
+          Cwd
+    end.
+
 build_name(Config) ->
+    %% generate a unique name for our test node, we want
+    %% to make sure the odds of name clashing are low
+    Random = integer_to_list(crypto:rand_uniform(0, 10000)),
     case rebar_config:get_local(Config, ct_use_short_names, false) of
-        true -> "-sname test";
-        false -> " -name test@" ++ net_adm:localhost()
+        true -> "-sname test" ++ Random;
+        false -> " -name test" ++ Random ++ "@" ++ net_adm:localhost()
     end.
 
 get_extra_params(Config) ->