Anonymous avatar Anonymous committed 6db9ac0

add 'make test' eunit target and some tests in webmachine_dispatcher

Comments (0)

Files changed (5)

 demo/ebin/webmachine_demo.app
 deps/mochiweb/ebin/mochiweb.app
 ebin/webmachine.app
+test\.log
+test/*
+
 
 ebin/$(APP).app: src/$(APP).app
 	@cp -v src/$(APP).app $@
+
+test: erl
+	scripts/run_tests.escript ebin | tee test.log
+

scripts/run_tests.escript

+#!/usr/bin/env escript
+%% -*- erlang -*-
+%%! -name wm_test@127.0.0.1
+main([Ebin]) ->
+    code:add_path(Ebin),
+    code:add_paths(filelib:wildcard("deps/*/ebin", Ebin)),
+    code:add_paths(filelib:wildcard("deps/*/deps/*/ebin", Ebin)),
+
+    {ok, [{application, webmachine, App}]} =
+        file:consult(filename:join([Ebin, "webmachine.app"])),
+    {ok, NonTestRe} = re:compile("_tests$"),
+    Modules = lists:filter(
+                fun(M) when M =:= gen_server2 ->
+                        false;
+                   (M) when M =:= priority_queue ->
+                        false;
+                   (M) when M =:= json_pp ->
+                        false;
+                   (M) ->
+                        nomatch == re:run(atom_to_list(M), NonTestRe)
+                end,
+                proplists:get_value(modules, App)),
+    
+    crypto:start(),
+    start_cover(Modules),
+    eunit:test(Modules, [verbose]),
+    analyze_cover(Modules);
+main(_) ->
+    io:format("usage: run_tests.escript RIAK_EBIN_DIRECTORY~n"),
+    halt(1).
+
+start_cover(Modules) ->
+    {ok, _Cover} = cover:start(),
+    io:format("Cover compiling...~n"),
+    Compiled = [ M || {ok, M} <- [ cover:compile(
+                                     filename:join(["src",atom_to_list(M)]))
+                                   || M <- Modules ] ],
+    case length(Modules) == length(Compiled) of
+        true -> ok;
+        false ->
+            io:format("Warning: the following modules were not"
+                      " cover-compiled:~n   ~p~n", [Compiled])
+    end.
+
+analyze_cover(Modules) ->
+    io:format("Analyzing cover...~n"),
+    CoverBase = filename:join(["test", "cover"]),
+    ok = filelib:ensure_dir(filename:join([CoverBase, "fake"])),
+    Coverages = lists:foldl(
+                  fun(M, Acc) ->
+                          [analyze_module(CoverBase, M)|Acc]
+                  end,
+                  [], Modules),
+    IndexFilename = filename:join([CoverBase, "index.html"]),
+    {ok, Index} = file:open(IndexFilename, [write]),
+    {LineTotal, CoverTotal} = 
+        lists:foldl(fun({_,_,Lines,Covered}, {LineAcc, CovAcc}) ->
+                            {LineAcc+Lines, CovAcc+Covered}
+                    end, {0,0}, Coverages),
+    file:write(Index,
+               "<html><head><title>Riak Coverage</title></head>\n"
+               "<body><h1>Riak Coverage</h1><ul>\n"),
+    file:write(Index,
+               io_lib:format("<h2>Total: ~.2f%</h2>\n",
+                             [percentage(CoverTotal, LineTotal)])),
+    [ file:write(Index,
+                 io_lib:format(
+                   "<li><a href=\"~s\">~p</a>: ~.2f%</li>~n",
+                   [Filename, Module, percentage(Covered, Lines)]))
+      || {Filename, Module, Lines, Covered} <- Coverages ],
+    file:write(Index,"</ul></body></html>"),
+    file:close(Index),
+    io:format("Cover analysis in ~s~n", [IndexFilename]).
+
+analyze_module(CoverBase, Module) ->
+    {ok, Filename} = 
+        cover:analyze_to_file(
+          Module,
+          filename:join(CoverBase, atom_to_list(Module)++".COVER.html"),
+          [html]),
+    Lines = count_lines(Filename, "[[:digit:]]\.\.|"),
+    Covered = count_lines(Filename, "[[:space:]]0\.\.|"),
+    {filename:basename(Filename), Module, Lines, Lines-Covered}.
+
+count_lines(Filename, Pattern) ->
+    {ok, [Lines],_} = io_lib:fread(
+                        "~d",
+                        os:cmd(io_lib:format("grep -e \"~s\" ~s | wc -l",
+                                             [Pattern, Filename]))),
+    Lines.
+
+percentage(_, 0) -> 1000.0;
+percentage(Part, Total) ->
+    (Part/Total)*100.
+

src/webmachine_dispatcher.erl

 -author('Bryan Fink <bryan@basho.com>').
 
 -export([dispatch/2, dispatch/3]).
+-include_lib("eunit/include/eunit.hrl").
 
 -define(SEPARATOR, $\/).
 -define(MATCH_ALL, '*').
 calculate_app_root(1) -> ".";
 calculate_app_root(N) when N > 1 ->
     string:join(lists:duplicate(N, ".."), [?SEPARATOR]).
+
+%%
+%% TEST
+%%
+
+app_root_test() ->
+    ?assertEqual(".",           calculate_app_root(1)),
+    ?assertEqual("../..",       calculate_app_root(2)),
+    ?assertEqual("../../..",    calculate_app_root(3)),
+    ?assertEqual("../../../..", calculate_app_root(4)).
+
+reconstitute_test() ->
+    ?assertEqual("",            reconstitute([])),
+    ?assertEqual("foo",         reconstitute(["foo"])),
+    ?assertEqual("foo/bar",     reconstitute(["foo","bar"])),
+    ?assertEqual("foo/bar/baz", reconstitute(["foo","bar","baz"])).
+
+split_host_test() ->
+    ?assertEqual(["foo","bar","baz"], split_host("foo.bar.baz")).
+
+split_host_port_test() ->
+    ?assertEqual({[], 80}, split_host_port("")),
+    ?assertEqual({["foo","bar","baz"], 80},
+                 split_host_port("foo.bar.baz:80")),
+    ?assertEqual({["foo","bar","baz"], 1234},
+                 split_host_port("foo.bar.baz:1234")).
+
+bind_empty_test() ->
+    ?assertEqual({ok, [], [], 0}, bind([], [], [], 0)),
+    ?assertEqual({ok, [], [{x,"a"}], 1},
+                 bind([], [], [{x,"a"}], 1)).
+
+bind_simple_matchall_test() ->
+    ?assertEqual({ok, [], [], 1},
+                 bind([?MATCH_ALL], [], [], 1)),
+    ?assertEqual({ok, ["a","b"], [], 2},
+                 bind([?MATCH_ALL], ["a","b"], [], 0)).

Empty file added.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.