diff --git a/lib/elixir/lib/code.ex b/lib/elixir/lib/code.ex index e5f8be14a0..f01582b08d 100644 --- a/lib/elixir/lib/code.ex +++ b/lib/elixir/lib/code.ex @@ -17,6 +17,16 @@ defmodule Code do server_call :loaded end + @doc """ + Removes the given files from the loaded files list. + The modules defined in the file are not removed, + calling this function only removes it from the list, + allowing it to be required again. + """ + def unload_files(files) do + server_call { :unload_files, files } + end + @doc """ Appends a path to Erlang VM code path. The path is expanded with `File.expand_path` before added. diff --git a/lib/elixir/src/elixir_code_server.erl b/lib/elixir/src/elixir_code_server.erl index bb4f307a46..b4ca587eb8 100644 --- a/lib/elixir/src/elixir_code_server.erl +++ b/lib/elixir/src/elixir_code_server.erl @@ -43,13 +43,23 @@ handle_call({ loaded, Path }, _From, Config) -> { reply, ok, Config#elixir_code_server{loaded=Done} } end; -handle_call({at_exit, AtExit}, _From, Config) -> +handle_call({ unload_files, Files }, _From, Config) -> + Current = Config#elixir_code_server.loaded, + Unloaded = lists:foldl(fun(File, Acc) -> + case orddict:find(File, Acc) of + { ok, true } -> orddict:erase(File, Acc); + _ -> Acc + end + end, Current, Files), + { reply, ok, Config#elixir_code_server{loaded=Unloaded} }; + +handle_call({ at_exit, AtExit }, _From, Config) -> { reply, ok, Config#elixir_code_server{at_exit=[AtExit|Config#elixir_code_server.at_exit]} }; -handle_call({argv, Argv}, _From, Config) -> +handle_call({ argv, Argv }, _From, Config) -> { reply, ok, Config#elixir_code_server{argv=Argv} }; -handle_call({compiler_options, Options}, _From, Config) -> +handle_call({ compiler_options, Options }, _From, Config) -> Final = orddict:merge(fun(_,_,V) -> V end, Config#elixir_code_server.compiler_options, Options), { reply, ok, Config#elixir_code_server{compiler_options=Final} }; diff --git a/lib/elixir/test/elixir/code_test.exs b/lib/elixir/test/elixir/code_test.exs index 3706dfaf69..cb24541ec0 100644 --- a/lib/elixir/test/elixir/code_test.exs +++ b/lib/elixir/test/elixir/code_test.exs @@ -29,8 +29,13 @@ defmodule CodeTest do end test :require do - Code.require_file fixture_path("code_sample"), __FILE__ + Code.require_file fixture_path("code_sample") assert fixture_path("code_sample.exs") in Code.loaded_files + assert Code.require_file(fixture_path("code_sample")) == nil + + Code.unload_files [fixture_path("code_sample.exs")] + refute fixture_path("code_sample.exs") in Code.loaded_files + assert Code.require_file(fixture_path("code_sample")) != nil end test :file do