Permalink
Browse files

add simple test for making a copy of a torrent

  • Loading branch information...
1 parent 42c938f commit 4a7145f7d7b843a83bd054cb681055dc62450c8a Magnus Klaar committed Aug 10, 2011
@@ -4,7 +4,8 @@
%% exported functions
-export([make/0,
- torrents/0]).
+ torrents/0,
+ copy/1]).
%% private functions
-export([exists/1]).
@@ -30,9 +31,21 @@ make_(Dir) ->
%% @doc List all available torrent files.
%% @end
--spec torrents() -> [Filenames::string()].
+-spec torrents() -> {ok, [Filenames::string()]} | {error, noent}.
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.
-spec exists(Path::string()) -> boolean().
@@ -61,20 +74,32 @@ teardown_config(_Dir) ->
dotfiles_test_() ->
{setup,local,
fun() -> application:start(gproc) end,
- fun(_) -> application:stop(gproc) end,
- {foreach, local,
- fun setup_config/0,
- fun teardown_config/1, [
- ?_test(test_no_torrents()),
- ?_test(test_ensure_exists())
- ]}}.
+ fun(_) -> application:stop(gproc) end, [
+ {foreach,local,
+ fun setup_config/0,
+ fun teardown_config/1, [
+ ?_test(test_no_torrents()),
+ ?_test(test_ensure_exists()),
+ {setup,local,
+ fun() -> ?MODULE:make() end,
+ fun(_) -> ok end, [
+ ?_test(test_copy_torrent())
+ ]}
+ ]}
+ ]}.
test_no_torrents() ->
- ?assertEqual([], ?MODULE:torrents()).
+ ?assertEqual({error, enoent}, ?MODULE:torrents()).
test_ensure_exists() ->
?assertNot(?MODULE:exists(gproc:get_env(l, etorrent, dotdir))),
ok = ?MODULE:make(),
?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.

0 comments on commit 4a7145f

Please sign in to comment.