Browse Source

Add test-cases for mad_utils module

Remove 'export_all'
Sina Samavati 11 years ago
parent
commit
7aa1a364d1

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 ebin
 *.beam
 erl_crash.dump
+logs

+ 173 - 10
Makefile

@@ -1,21 +1,184 @@
-.PHONY: all compile clean build
+PROJECT = mad
+CT_SUITES = mad_utils
 
-all: clean compile build
+.PHONY: all build
 
-clean:
-	rm -rf ebin
+all: app build
+
+build:
+	escript build
+
+
+# github.com/extend/erlang.mk
+# Copyright (c) 2013, Loïc Hoguin <essen@ninenines.eu>
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
-erlc_verbose_0 = @echo " ERLC  " $(filter %.erl ,$(?F));
+# Project.
+
+PROJECT ?= $(notdir $(CURDIR))
+
+# Verbosity and tweaks.
+
+V ?= 0
+
+appsrc_verbose_0 = @echo " APP   " $(PROJECT).app.src;
+appsrc_verbose = $(appsrc_verbose_$(V))
+
+erlc_verbose_0 = @echo " ERLC  " $(filter %.erl %.core,$(?F));
 erlc_verbose = $(erlc_verbose_$(V))
 
+xyrl_verbose_0 = @echo " XYRL  " $(filter %.xrl %.yrl,$(?F));
+xyrl_verbose = $(xyrl_verbose_$(V))
+
+dtl_verbose_0 = @echo " DTL   " $(filter %.dtl,$(?F));
+dtl_verbose = $(dtl_verbose_$(V))
+
+gen_verbose_0 = @echo " GEN   " $@;
+gen_verbose = $(gen_verbose_$(V))
+
+.PHONY: app clean clean-all clean-docs docs build-tests test build-plt dialyze
+
+# Deps directory.
+
+DEPS_DIR ?= $(CURDIR)/deps
+export DEPS_DIR
+
+REBAR_DEPS_DIR = $(DEPS_DIR)
+export REBAR_DEPS_DIR
+
+ALL_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(DEPS))
+ALL_TEST_DEPS_DIRS = $(addprefix $(DEPS_DIR)/,$(TEST_DEPS))
+
+# Application.
+
+ERL_LIBS ?= $(DEPS_DIR)
+export ERL_LIBS
+
+ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \
+	+warn_shadow_vars +warn_obsolete_guard # +bin_opt_info +warn_missing_spec
+COMPILE_FIRST ?=
+COMPILE_FIRST_PATHS = $(addprefix src/,$(addsuffix .erl,$(COMPILE_FIRST)))
+
+clean-all: clean clean-docs
+	$(gen_verbose) rm -rf .$(PROJECT).plt $(DEPS_DIR) logs
+
+app: ebin/$(PROJECT).app
+	$(eval MODULES := $(shell find ebin -type f -name \*.beam \
+		| sed 's/ebin\///;s/\.beam/,/' | sed '$$s/.$$//'))
+	$(appsrc_verbose) cat src/$(PROJECT).app.src \
+		| sed 's/{modules, \[\]}/{modules, \[$(MODULES)\]}/' \
+		> ebin/$(PROJECT).app
+
 define compile_erl
-	$(erlc_verbose) erlc -v -o ebin/ $(1)
+	$(erlc_verbose) erlc -v $(ERLC_OPTS) -o ebin/ \
+		-pa ebin/ -I include/ $(COMPILE_FIRST_PATHS) $(1)
 endef
 
-compile: $(shell find src -type f -name \*.erl)
+define compile_xyrl
+	$(xyrl_verbose) erlc -v -o ebin/ $(1)
+	$(xyrl_verbose) erlc $(ERLC_OPTS) -o ebin/ ebin/*.erl
+	@rm ebin/*.erl
+endef
+
+define compile_dtl
+	$(dtl_verbose) erl -noshell -pa ebin/ $(DEPS_DIR)/erlydtl/ebin/ -eval ' \
+		Compile = fun(F) -> \
+			Module = list_to_atom( \
+				string:to_lower(filename:basename(F, ".dtl")) ++ "_dtl"), \
+			erlydtl_compiler:compile(F, Module, [{out_dir, "ebin/"}]) \
+		end, \
+		_ = [Compile(F) || F <- string:tokens("$(1)", " ")], \
+		init:stop()'
+endef
+
+ebin/$(PROJECT).app: $(shell find src -type f -name \*.erl) \
+		$(shell find src -type f -name \*.core) \
+		$(shell find src -type f -name \*.xrl) \
+		$(shell find src -type f -name \*.yrl) \
+		$(shell find templates -type f -name \*.dtl 2>/dev/null)
 	@mkdir -p ebin/
-	$(if $(strip $(filter %.erl ,$?)), \
+	$(if $(strip $(filter %.erl %.core,$?)), \
 		$(call compile_erl,$(filter %.erl %.core,$?)))
+	$(if $(strip $(filter %.xrl %.yrl,$?)), \
+		$(call compile_xyrl,$(filter %.xrl %.yrl,$?)))
+	$(if $(strip $(filter %.dtl,$?)), \
+		$(call compile_dtl,$(filter %.dtl,$?)))
 
-build:
-	escript build
+clean:
+	$(gen_verbose) rm -rf ebin/ test/*.beam erl_crash.dump
+
+# Documentation.
+
+docs: clean-docs
+	$(gen_verbose) erl -noshell \
+		-eval 'edoc:application($(PROJECT), ".", []), init:stop().'
+
+clean-docs:
+	$(gen_verbose) rm -f doc/*.css doc/*.html doc/*.png doc/edoc-info
+
+# Tests.
+
+$(foreach dep,$(TEST_DEPS),$(eval $(call dep_target,$(dep))))
+
+build-test-deps: $(ALL_TEST_DEPS_DIRS)
+	@for dep in $(ALL_TEST_DEPS_DIRS) ; do $(MAKE) -C $$dep; done
+
+build-tests: build-test-deps
+	$(gen_verbose) erlc -v $(ERLC_OPTS) -o test/ \
+		$(wildcard test/*.erl test/*/*.erl) -pa ebin/
+
+CT_RUN = ct_run \
+	-no_auto_compile \
+	-noshell \
+	-pa $(realpath ebin) $(DEPS_DIR)/*/ebin \
+	-dir test \
+	-logdir logs
+#	-cover test/cover.spec
+
+CT_SUITES ?=
+
+define test_target
+test_$(1): ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
+test_$(1): clean deps app build-tests
+	@if [ -d "test" ] ; \
+	then \
+		mkdir -p logs/ ; \
+		$(CT_RUN) -suite $(addsuffix _SUITE,$(1)) ; \
+	fi
+	$(gen_verbose) rm -f test/*.beam
+endef
+
+$(foreach test,$(CT_SUITES),$(eval $(call test_target,$(test))))
+
+test: ERLC_OPTS += -DTEST=1 +'{parse_transform, eunit_autoexport}'
+test: app build-tests
+	@if [ -d "test" ] ; \
+	then \
+		mkdir -p logs/ ; \
+		$(CT_RUN) -suite $(addsuffix _SUITE,$(CT_SUITES)) ; \
+	fi
+	$(gen_verbose) rm -f test/*.beam
+
+# Dialyzer.
+
+PLT_APPS ?=
+DIALYZER_OPTS ?= -Werror_handling -Wrace_conditions \
+	-Wunmatched_returns # -Wunderspecs
+
+build-plt: deps app
+	@dialyzer --build_plt --output_plt .$(PROJECT).plt \
+		--apps erts kernel stdlib $(PLT_APPS) -r deps/*/ebin
+
+dialyze:
+	@dialyzer --src src --plt .$(PROJECT).plt --no_native $(DIALYZER_OPTS)

+ 1 - 1
src/mad_compile.erl

@@ -2,7 +2,7 @@
 
 -export([deps/1]).
 -export([app/1]).
--compile(export_all).
+
 -include("mad.hrl").
 
 

+ 15 - 14
src/mad_utils.erl

@@ -2,13 +2,12 @@
 
 -export([cwd/0]).
 -export([exec/2]).
--export([concat/1]).
 -export([home/0]).
+-export([consult/1]).
 -export([rebar_conf/1]).
 -export([src/1]).
 -export([include/1]).
 -export([ebin/1]).
--export([consult/1]).
 -export([deps/1]).
 -export([get_value/3]).
 -export([script/2]).
@@ -29,15 +28,21 @@ exec(Cmd, Opts) ->
     Opts1 = [concat([" ", X]) || X <- Opts],
     os:cmd(concat([Cmd, concat(Opts1)])).
 
-concat(L) ->
-    lists:concat(L).
-
 %% return $HOME
 home() ->
     %% ~/
     {ok, [[H|_]]} = init:get_argument(home),
     H.
 
+consult(File) ->
+    AbsFile = filename:absname(File),
+    case file:consult(AbsFile) of
+        {ok, V} ->
+            V;
+        _ ->
+            []
+    end.
+
 rebar_conf(Dir) ->
     Dir1 = filename:absname(Dir),
     consult(filename:join(Dir1, "rebar.config")).
@@ -54,15 +59,6 @@ ebin(Dir) ->
     %% Dir/ebin
     filename:join(Dir, "ebin").
 
-consult(File) ->
-    AbsFile = filename:absname(File),
-    case file:consult(AbsFile) of
-        {ok, V} ->
-            V;
-        _ ->
-            []
-    end.
-
 deps(File) ->
     get_value(deps, consult(File), []).
 
@@ -116,3 +112,8 @@ last_modified(File) ->
         Else ->
             calendar:datetime_to_gregorian_seconds(Else)
     end.
+
+
+%% internal
+concat(L) ->
+    lists:concat(L).

+ 10 - 0
test/helper.erl

@@ -0,0 +1,10 @@
+-module(helper).
+-export([get_value/2]).
+
+get_value(Key, Conf) ->
+    case lists:keyfind(Key, 1, Conf) of
+        {Key, Value} ->
+            Value;
+        _ ->
+            undefined
+    end.

+ 10 - 0
test/mad_deps_SUITE.erl

@@ -0,0 +1,10 @@
+-module(mad_deps_SUITE).
+
+-export([all/0]).
+-export([container/1]).
+
+all() ->
+    [container].
+
+container(_) ->
+    true = (mad_deps:container() =:= "").

+ 69 - 0
test/mad_utils_SUITE.erl

@@ -0,0 +1,69 @@
+-module(mad_utils_SUITE).
+
+-export([all/0]).
+-export([cwd/1]).
+-export([home/1]).
+-export([exec/1]).
+-export([consult/1]).
+-export([rebar_conf/1]).
+-export([src/1]).
+-export([include/1]).
+-export([ebin/1]).
+-export([deps/1]).
+-export([script/1]).
+
+-import(helper, [get_value/2]).
+
+
+all() ->
+    [cwd, exec, home, consult, rebar_conf, src, include, ebin, deps, script].
+
+cwd(_) ->
+    Cwd = os:cmd("pwd") -- "\n",
+    Cwd = mad_utils:cwd().
+
+exec(_) ->
+    "xyz" = mad_utils:exec("echo", ["-n", "xyz"]).
+
+home(_) ->
+    Home = os:cmd("echo $HOME") -- "\n",
+    Home = mad_utils:home().
+
+consult(Config) ->
+    File = filename:join(get_value(data_dir, Config), "rebar"),
+    [] = mad_utils:consult(File),
+    [{deps, [
+             {mad, ".*", {git, "git://github.com/s1n4/mad.git",
+                          {branch, "master"}}}
+            ]},
+     {erl_opts, [d, 'X']}] = mad_utils:consult(File ++ ".config").
+
+rebar_conf(Config) ->
+    [] = mad_utils:rebar_conf("."),
+    [{deps, [
+             {mad, ".*", {git, "git://github.com/s1n4/mad.git",
+                          {branch, "master"}}}
+            ]},
+     {erl_opts, [d, 'X']}] = mad_utils:rebar_conf(get_value(data_dir, Config)).
+
+src(_) ->
+    "/path/to/app/src" = mad_utils:src("/path/to/app").
+
+include(_) ->
+    "/path/to/app/include" = mad_utils:include("/path/to/app").
+
+ebin(_) ->
+    "/path/to/app/ebin" = mad_utils:ebin("/path/to/app").
+
+deps(Config) ->
+    File = filename:join(get_value(data_dir, Config), "rebar"),
+    [] = mad_utils:deps(File),
+    [{mad, ".*",
+      {git, "git://github.com/s1n4/mad.git", {branch, "master"}
+      }}] = mad_utils:deps(File ++ ".config").
+
+script(Config) ->
+    [a, b, c] = mad_utils:script(mad_utils:cwd(), [a, b, c]),
+    Dir = get_value(data_dir, Config),
+    [{sub_dirs, ["sub_dir1", "sub_dir2"]},
+     a, b, c] = mad_utils:script(Dir, [a, b, c]).

+ 4 - 0
test/mad_utils_SUITE_data/rebar.config

@@ -0,0 +1,4 @@
+{deps, [
+        {mad, ".*", {git, "git://github.com/s1n4/mad.git", {branch, "master"}}}
+       ]}.
+{erl_opts, [d, 'X']}.

+ 1 - 0
test/mad_utils_SUITE_data/rebar.config.script

@@ -0,0 +1 @@
+[{sub_dirs, ["sub_dir1", "sub_dir2"]}|CONFIG].