diff --git a/apps/etorrent/src/etorrent_dotfiles.erl b/apps/etorrent/src/etorrent_dotfiles.erl index 174b98cb..85257052 100644 --- a/apps/etorrent/src/etorrent_dotfiles.erl +++ b/apps/etorrent/src/etorrent_dotfiles.erl @@ -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. diff --git a/test/etorrent_eunit_SUITE_data/debian-6.0.2.1-amd64-netinst.iso.torrent b/test/etorrent_eunit_SUITE_data/debian-6.0.2.1-amd64-netinst.iso.torrent new file mode 100644 index 00000000..cc38e5e7 Binary files /dev/null and b/test/etorrent_eunit_SUITE_data/debian-6.0.2.1-amd64-netinst.iso.torrent differ