[erlang-questions] EUnit setUp/tearDown question

Gianfranco Alongi gianfranco.alongi@REDACTED
Mon Oct 25 11:08:39 CEST 2010


Hi,


-module(wth).
-include_lib("eunit/include/eunit.hrl").

is_link_test_() ->
    {foreach,
     fun() ->  ?cmd("mktemp -d temp") -- "\n"  end,
     fun(T) -> ?cmd("rm -rf " ++ T) end,
     [
      fun test_is_link_with_regular/1,
      fun test_is_link_with_link/1
     ]
    }.

% ------------------------------------------------------------------------------
is_link(P) ->
    {ok, FIT} = file:read_link_info(P),
    element(3,FIT) =:= symlink.
  
test_is_link_with_regular(T) ->
    {"Test is link with regular",
    fun() ->
	    P = filename:join(T, "a"),
	    ?cmd("touch "++P),
	    ?assert(not is_link(P))
    end}.

test_is_link_with_link(T) ->
    {"Test Is link with Link",
     fun() ->
	     P = filename:join(T, "a"),
	     ?cmd("touch "++P),
	     ?cmd("ln -s "++P++" "++P++".link"),
	     ?assert(is_link(P ++ ".link"))
     end}.



 zenon$ erlc wth.erl 
 zenon$ erl -pa -eval 'eunit:test(wth,[verbose]).'

Erlang R13B04 (erts-5.7.5) [source] [64-bit] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.5  (abort with ^G)
1> ======================== EUnit ========================
module 'wth'
  wth: test_is_link_with_regular (Test is link with regular)...[0.008 s] ok
  wth: test_is_link_with_link (Test Is link with Link)...[0.011 s] ok
  [done in 0.049 s]
=======================================================
  All 2 tests passed.

1> 




----- Original Message -----
From: "Muharem Hrnjadovic" <muharem@REDACTED>
To: erlang-questions@REDACTED
Sent: Monday, October 25, 2010 9:33:25 AM GMT +00:00 GMT Britain, Ireland, Portugal
Subject: [erlang-questions] EUnit setUp/tearDown question

Hello there!

I am trying to set up a suite of tests where a temporary directory is
created and torn down for each test.

{{{
  1 -module(eunit_tests).
  2
  3 -include_lib("eunit.hrl").
  4
  5 is_link(P) ->
  6     {ok, FIT} = file:read_link_info(P),
  7     element(3,FIT) =:= symlink.
  8
  9 test_is_link_with_regular(T) ->
 10     P = filename:join(T, "a"),
 11     ?cmd(io_lib:format("touch ~s", [P])),
 12     ?assert(not is_link(P)).
 13
 14 test_is_link_with_link(T) ->
 15     P = filename:join(T, "a"),
 16     ?cmd(io_lib:format("touch ~s", [P])),
 17     ?cmd(io_lib:format("ln -s ~s ~s.link", [P, P])),
 18     ?assert(is_link(P ++ ".link")).
 19
 20 is_link_test_() ->
 21     {foreach,
 22      fun() -> ?cmd("mktemp -d") end,
 23      fun(T) -> ?cmd("rm -rf " ++ T) end,
 24      ...
 25     }.
}}}

My question is: what should go into line 24 so that the two tests
(test_is_link_with_regular() and test_is_link_with_link()) are called
with T (the temporary directory path)?

Or is there a better way to achieve the same thing?

Best regards/Mit freundlichen Grüßen

-- 
Muharem Hrnjadovic <muharem@REDACTED>
Public key id   : B2BBFCFC
Key fingerprint : A5A3 CC67 2B87 D641 103F  5602 219F 6B60 B2BB FCFC



More information about the erlang-questions mailing list