Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add simple test for making a copy of a torrent
  • Loading branch information
beapirate committed Aug 10, 2011
1 parent 42c938f commit 4a7145f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions apps/etorrent/src/etorrent_dotfiles.erl
Expand Up @@ -4,7 +4,8 @@


%% exported functions %% exported functions
-export([make/0, -export([make/0,
torrents/0]). torrents/0,
copy/1]).


%% private functions %% private functions
-export([exists/1]). -export([exists/1]).
Expand All @@ -30,9 +31,21 @@ make_(Dir) ->


%% @doc List all available torrent files. %% @doc List all available torrent files.
%% @end %% @end
-spec torrents() -> [Filenames::string()]. -spec torrents() -> {ok, [Filenames::string()]} | {error, noent}.
torrents() -> torrents() ->
[]. file:list_dir(gproc:get_env(l, etorrent, dotdir)).

%% @doc Make a private copy of a torrent file.
%% @end
-spec copy(Torrentfile::string()) -> ok.
copy(Torrentfile) when is_list(Torrentfile) ->
File = filename:basename(Torrentfile),
Dotdir = gproc:get_env(l, etorrent, dotdir),
Dest = filename:join([Dotdir, File]),
case file:copy(Torrentfile, Dest) of
{ok, _} -> ok;
{error, _}=Error -> Error
end.


%% @private Check if a file path exists. %% @private Check if a file path exists.
-spec exists(Path::string()) -> boolean(). -spec exists(Path::string()) -> boolean().
Expand Down Expand Up @@ -61,20 +74,32 @@ teardown_config(_Dir) ->
dotfiles_test_() -> dotfiles_test_() ->
{setup,local, {setup,local,
fun() -> application:start(gproc) end, fun() -> application:start(gproc) end,
fun(_) -> application:stop(gproc) end, fun(_) -> application:stop(gproc) end, [
{foreach, local, {foreach,local,
fun setup_config/0, fun setup_config/0,
fun teardown_config/1, [ fun teardown_config/1, [
?_test(test_no_torrents()), ?_test(test_no_torrents()),
?_test(test_ensure_exists()) ?_test(test_ensure_exists()),
]}}. {setup,local,
fun() -> ?MODULE:make() end,
fun(_) -> ok end, [
?_test(test_copy_torrent())
]}
]}
]}.


test_no_torrents() -> test_no_torrents() ->
?assertEqual([], ?MODULE:torrents()). ?assertEqual({error, enoent}, ?MODULE:torrents()).


test_ensure_exists() -> test_ensure_exists() ->
?assertNot(?MODULE:exists(gproc:get_env(l, etorrent, dotdir))), ?assertNot(?MODULE:exists(gproc:get_env(l, etorrent, dotdir))),
ok = ?MODULE:make(), ok = ?MODULE:make(),
?assert(?MODULE:exists(gproc:get_env(l, etorrent, dotdir))). ?assert(?MODULE:exists(gproc:get_env(l, etorrent, dotdir))).


test_copy_torrent() ->
c:pwd(),
Torrent = "../../../test/etorrent_eunit_SUITE_data/debian-6.0.2.1-amd64-netinst.iso.torrent",
ok = ?MODULE:copy(Torrent),
?assertEqual({ok, ["debian-6.0.2.1-amd64-netinst.iso.torrent"]}, ?MODULE:torrents()).

-endif. -endif.
Binary file not shown.

0 comments on commit 4a7145f

Please sign in to comment.