From 25538e071fa154b49a8f282e84907316b1bf54b6 Mon Sep 17 00:00:00 2001 From: Chris Bernard Date: Thu, 4 Mar 2010 20:53:12 -0500 Subject: [PATCH] Fixed 3 bugs in rebar_eunit. Added EUnit tests to capture them. 1. When running the eunit command with the convention of putting tests in "*_tests" modules, eunit would run those tests twice. This is because: 1) eunit:test/1 will naturally look for foo's tests both in foo, and in foo_tests, and 2) eunit:test/1 was being folded over all project modules. The fix is to filter "*_tests" modules from the list passed to eunit:test/1. 2. When running the eunit command with cover enabled and tests in a 'test' directory, cover would error because it couldn't find the source code for those tests. This is because cover:analyze/3 will only find module source in "." and "../src". This is hard-coded in cover :-(. Since cover shouldn't be calculating code coverage on test code anyway, the fix is to not fold cover:analyze/3 over non-production code. 3. When running the eunit command with cover enabled and a test suite defined, cover would only attempt to calculate coverage on the the test suite itself. This was because only the suite was passed to cover:analyze/3. The fix is to fold cover:analyze/3 over all the production code, filtering out the suite module if it is defined. --- src/rebar_eunit.erl | 21 ++-- test/rebar_eunit_tests.erl | 229 +++++++++++++++++++++++++++++++++++++ 2 files changed, 240 insertions(+), 10 deletions(-) create mode 100644 test/rebar_eunit_tests.erl diff --git a/src/rebar_eunit.erl b/src/rebar_eunit.erl index 38c1001..33a6ba7 100644 --- a/src/rebar_eunit.erl +++ b/src/rebar_eunit.erl @@ -72,12 +72,15 @@ eunit(Config, _File) -> %% and eunit testing. Normally you can just tell cover and/or eunit to %% scan the directory for you, but eunit does a code:purge in conjunction %% with that scan and causes any cover compilation info to be lost. - BeamFiles = rebar_utils:beams(?EUNIT_DIR), + %% Filter out "*_tests" modules so eunit won't doubly run them and + %% so cover only calculates coverage on production code. + BeamFiles = [N || N <- rebar_utils:beams(?EUNIT_DIR), + string:str(N, "_tests.beam") =:= 0], Modules = [rebar_utils:beam_to_mod(?EUNIT_DIR, N) || N <- BeamFiles], cover_init(Config, BeamFiles), EunitResult = perform_eunit(Config, Modules), - perform_cover(Config, BeamFiles), + perform_cover(Config, Modules), case EunitResult of ok -> @@ -176,18 +179,16 @@ perform_cover(Config, BeamFiles) -> perform_cover(false, _Config, _BeamFiles) -> ok; perform_cover(true, Config, BeamFiles) -> - perform_cover(Config, BeamFiles, rebar_config:get_global(suite, undefined)); -perform_cover(Config, BeamFiles, undefined) -> - cover_analyze(Config, BeamFiles); -perform_cover(Config, _BeamFiles, Suite) -> - cover_analyze(Config, [filename:join([?EUNIT_DIR | string:tokens(Suite, ".")]) ++ ".beam"]). + cover_analyze(Config, BeamFiles). cover_analyze(_Config, []) -> ok; -cover_analyze(_Config, BeamFiles) -> - Modules = [rebar_utils:beam_to_mod(?EUNIT_DIR, N) || N <- BeamFiles], +cover_analyze(_Config, Modules) -> + Suite = list_to_atom(rebar_config:get_global(suite, "")), + FilteredModules = [M || M <- Modules, M =/= Suite], + %% Generate coverage info for all the cover-compiled modules - Coverage = [cover_analyze_mod(M) || M <- Modules], + Coverage = [cover_analyze_mod(M) || M <- FilteredModules], %% Write index of coverage info cover_write_index(lists:sort(Coverage)), diff --git a/test/rebar_eunit_tests.erl b/test/rebar_eunit_tests.erl new file mode 100644 index 0000000..4f035fb --- /dev/null +++ b/test/rebar_eunit_tests.erl @@ -0,0 +1,229 @@ +%% -*- tab-width: 4;erlang-indent-level: 4;indent-tabs-mode: nil -*- +%% ex: ts=4 sw=4 et +%% ------------------------------------------------------------------- +%% +%% rebar: Erlang Build Tools +%% +%% Copyright (c) 2009, 2010 Dave Smith (dizzyd@dizzyd.com) +%% +%% Permission is hereby granted, free of charge, to any person obtaining a copy +%% of this software and associated documentation files (the "Software"), to deal +%% in the Software without restriction, including without limitation the rights +%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +%% copies of the Software, and to permit persons to whom the Software is +%% furnished to do so, subject to the following conditions: +%% +%% The above copyright notice and this permission notice shall be included in +%% all copies or substantial portions of the Software. +%% +%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +%% THE SOFTWARE. +%% ------------------------------------------------------------------- +%% @author Chris Bernard +%% @doc This tests functionality provided by the rebar command 'eunit'. +%% @copyright 2009, 2010 Dave Smith +%% ------------------------------------------------------------------- +-module(rebar_eunit_tests). + +-compile(export_all). + +-include_lib("eunit/include/eunit.hrl"). + +%% Assuming this test is run inside the rebar 'eunit' +%% command, the current working directory will be '.eunit' +-define(REBAR_SCRIPT, "../rebar"). + +-define(TMP_DIR, "tmp_eunit/"). + +%% ==================================================================== +%% Rebar EUnit and Cover Tests +%% ==================================================================== + +eunit_test_() -> + {"Ensure EUnit runs with tests in a 'test' dir and no defined suite", + setup, fun() -> setup_basic_project(), rebar("-v eunit") end, + fun teardown/1, + fun(RebarOut) -> + [{"Tests in 'test' directory are found and run", + ?_assert(string:str(RebarOut, "myapp_mymod_tests:") =/= 0)}, + + {"Tests in 'src' directory are found and run", + ?_assert(string:str(RebarOut, "myapp_mymod:") =/= 0)}, + + {"Tests are only run once", + ?_assert(string:str(RebarOut, "All 2 tests passed") =/= 0)}] + end}. + +cover_test_() -> + {"Ensure Cover runs with tests in a test dir and no defined suite", + setup, fun() -> setup_cover_project(), rebar("-v eunit") end, + fun teardown/1, + + [{"All cover reports are generated", + assert_files_in("the temporary eunit directory", + expected_cover_generated_files())}, + + {"Only production modules get coverage reports", + assert_files_not_in("the temporary eunit directory", + [".eunit/myapp_mymod_tests.COVER.html"])}]}. + +cover_with_suite_test_() -> + {"Ensure Cover runs with Tests in a test dir and a test suite", + setup, + fun() -> + setup_cover_project_with_suite(), + rebar("-v eunit suite=mysuite") + end, + fun teardown/1, + + [{"All cover reports are generated", + assert_files_in("the temporary eunit directory", + expected_cover_generated_files())}, + + {"Only production modules get coverage reports", + assert_files_not_in("the temporary eunit directory", + [".eunit/myapp_mymod_tests.COVER.html", + ".eunit/mysuite"])}]}. + +expected_cover_generated_files() -> + [".eunit/index.html", + ".eunit/myapp_app.COVER.html", + ".eunit/myapp_mymod.COVER.html", + ".eunit/myapp_sup.COVER.html"]. + +%% ==================================================================== +%% Environment and Setup Tests +%% ==================================================================== + +environment_test_() -> + {"Sanity check the testing environment", + setup, fun make_tmp_dir/0, fun remove_tmp_dir/1, + + [{"Ensure a test project can be created", + ?_assert(filelib:is_dir(?TMP_DIR))}, + + {"Ensure the rebar script can be found, copied, and run", + [?_assert(filelib:is_file(?REBAR_SCRIPT)), + fun assert_rebar_runs/0]}]}. + +assert_rebar_runs() -> + prepare_rebar_script(), + ?assert(string:str(os:cmd("./" ++ ?TMP_DIR ++ "rebar"), "Usage: rebar") =/= 0). + +basic_setup_test_() -> + {"Create a simple project with a 'test' directory, a test, and a module", + setup, fun setup_basic_project/0, fun teardown/1, + + %% Test the setup function + assert_dirs_in("Basic Project", + ["src", "ebin", "test"]) ++ + assert_files_in("Basic Project", + ["test/myapp_mymod_tests.erl", "src/myapp_mymod.erl"])}. + +%% ==================================================================== +%% Setup and Teardown +%% ==================================================================== + +-define(myapp_mymod, + ["-module(myapp_mymod).\n", + "-export([myfunc/0]).\n", + "-include_lib(\"eunit/include/eunit.hrl\").\n", + "myfunc() -> ok.\n", + "myprivate_test() -> ?assert(true).\n"]). + +-define(myapp_mymod_tests, + ["-module(myapp_mymod_tests).\n", + "-compile([export_all]).\n", + "-include_lib(\"eunit/include/eunit.hrl\").\n", + "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]). + +-define(mysuite, + ["-module(mysuite).\n", + "-export([all_test_/0]).\n", + "-include_lib(\"eunit/include/eunit.hrl\").\n", + "all_test_() -> [myapp_mymod_defined_in_mysuite_tests].\n"]). + +-define(myapp_mymod_defined_in_mysuite_tests, + ["-module(myapp_mymod_defined_in_mysuite_tests).\n", + "-compile([export_all]).\n", + "-include_lib(\"eunit/include/eunit.hrl\").\n", + "myfunc_test() -> ?assertMatch(ok, myapp_mymod:myfunc()).\n"]). + +make_tmp_dir() -> + file:make_dir(?TMP_DIR). + +setup_environment() -> + make_tmp_dir(), + prepare_rebar_script(), + file:set_cwd(?TMP_DIR). + +setup_basic_project() -> + setup_environment(), + rebar("create-app appid=myapp"), + file:make_dir("test"), + file:write_file("test/myapp_mymod_tests.erl", ?myapp_mymod_tests), + file:write_file("src/myapp_mymod.erl", ?myapp_mymod). + +setup_cover_project() -> + setup_basic_project(), + file:write_file("rebar.config", "{cover_enabled, true}.\n"). + +setup_cover_project_with_suite() -> + setup_cover_project(), + file:write_file("test/mysuite.erl", ?mysuite), + file:write_file("test/myapp_mymod_defined_in_mysuite_tests.erl", + ?myapp_mymod_defined_in_mysuite_tests). + +teardown(_) -> + file:set_cwd(".."), + remove_tmp_dir(), + ok. + +remove_tmp_dir() -> + remove_tmp_dir(arg_for_eunit). + +remove_tmp_dir(_) -> + case os:type() of + {unix, _} -> + os:cmd("rm -rf " ++ ?TMP_DIR ++ " 2>/dev/null"); + {win32, _} -> + %% os:cmd("rmdir /S /Q " ++ ?TMP_DIR ++ " 2>NUL") + exit("Windows is not supported yet.") + end. + +%% ==================================================================== +%% Helper Functions +%% ==================================================================== + +prepare_rebar_script() -> + {ok, _} = file:copy(?REBAR_SCRIPT, ?TMP_DIR ++ "rebar"), + [] = os:cmd("chmod u+x " ++ ?TMP_DIR ++ "rebar"). + +rebar() -> + rebar([]). + +rebar(Args) when is_list(Args) -> + Out = os:cmd("./rebar " ++ Args), + %?debugMsg("**** Begin"), ?debugMsg(Out), ?debugMsg("**** End"), + Out. + +assert_dirs_in(Name, [Dir|T]) -> + [{Name ++ " has directory: " ++ Dir, ?_assert(filelib:is_dir(Dir))} | + assert_dirs_in(Name, T)]; +assert_dirs_in(_, []) -> []. + +assert_files_in(Name, [File|T]) -> + [{Name ++ " has file: " ++ File, ?_assert(filelib:is_file(File))} | + assert_files_in(Name, T)]; +assert_files_in(_, []) -> []. + +assert_files_not_in(Name, [File|T]) -> + [{Name ++ " does not have file: " ++ File, ?_assertNot(filelib:is_file(File))} | + assert_files_not_in(Name, T)]; +assert_files_not_in(_, []) -> []. +