@@ -263,30 +263,26 @@ defmodule MixTest do
263263 [
264264 { :git_repo , git: fixture_path ( "git_repo" ) }
265265 ] ,
266- lockfile: lockfile ,
267- verbose: true
266+ lockfile: lockfile
268267 )
269268
270269 assert_received { :mix_shell , :info , [ "* Getting git_repo " <> _ ] }
271- assert_received { :mix_shell , :info , [ "Mix.install/2 using " <> install_dir ] }
272- assert File . read! ( Path . join ( install_dir , "mix.lock" ) ) =~ rev
273- after
274- purge ( [ GitRepo , GitRepo.MixProject ] )
270+
271+ install_project_dir = Mix . install_project_dir ( )
272+ assert File . read! ( Path . join ( install_project_dir , "mix.lock" ) ) =~ rev
275273 end
276274
277275 test ":lockfile merging" , % { tmp_dir: tmp_dir } do
278276 [ rev1 , rev2 | _ ] = get_git_repo_revs ( "git_repo" )
279277
280- Mix . install (
281- [
282- { :git_repo , git: fixture_path ( "git_repo" ) }
283- ] ,
284- verbose: true
285- )
278+ Mix . install ( [
279+ { :git_repo , git: fixture_path ( "git_repo" ) }
280+ ] )
286281
287282 assert_received { :mix_shell , :info , [ "* Getting git_repo " <> _ ] }
288- assert_received { :mix_shell , :info , [ "Mix.install/2 using " <> install_dir ] }
289- assert File . read! ( Path . join ( install_dir , "mix.lock" ) ) =~ rev1
283+
284+ install_project_dir = Mix . install_project_dir ( )
285+ assert File . read! ( Path . join ( install_project_dir , "mix.lock" ) ) =~ rev1
290286
291287 Mix.Project . push ( GitApp )
292288 lockfile = Path . join ( tmp_dir , "lock" )
@@ -300,9 +296,7 @@ defmodule MixTest do
300296 lockfile: lockfile
301297 )
302298
303- assert File . read! ( Path . join ( install_dir , "mix.lock" ) ) =~ rev1
304- after
305- purge ( [ GitRepo , GitRepo.MixProject ] )
299+ assert File . read! ( Path . join ( install_project_dir , "mix.lock" ) ) =~ rev1
306300 end
307301
308302 test ":lockfile with application name" , % { tmp_dir: tmp_dir } do
@@ -318,15 +312,12 @@ defmodule MixTest do
318312 { :install_test , path: Path . join ( tmp_dir , "install_test" ) } ,
319313 { :git_repo , git: fixture_path ( "git_repo" ) }
320314 ] ,
321- lockfile: :install_test ,
322- verbose: true
315+ lockfile: :install_test
323316 )
324317
325318 assert_received { :mix_shell , :info , [ "* Getting git_repo " <> _ ] }
326- assert_received { :mix_shell , :info , [ "Mix.install/2 using " <> install_dir ] }
327- assert File . read! ( Path . join ( install_dir , "mix.lock" ) ) =~ rev
328- after
329- purge ( [ GitRepo , GitRepo.MixProject ] )
319+ install_project_dir = Mix . install_project_dir ( )
320+ assert File . read! ( Path . join ( install_project_dir , "mix.lock" ) ) =~ rev
330321 end
331322
332323 test ":lockfile that does not exist" do
@@ -335,6 +326,73 @@ defmodule MixTest do
335326 end
336327 end
337328
329+ test "restore dir" , % { tmp_dir: tmp_dir } do
330+ with_cleanup ( fn ->
331+ Mix . install ( [
332+ { :git_repo , git: fixture_path ( "git_repo" ) }
333+ ] )
334+
335+ assert_received { :mix_shell , :info , [ "* Getting git_repo " <> _ ] }
336+ assert_received { :mix_shell , :info , [ "==> git_repo" ] }
337+ assert_received { :mix_shell , :info , [ "Compiling 1 file (.ex)" ] }
338+ assert_received { :mix_shell , :info , [ "Generated git_repo app" ] }
339+ refute_received _
340+
341+ install_project_dir = Mix . install_project_dir ( )
342+ build_lib_path = Path . join ( [ install_project_dir , "_build" , "dev" , "lib" ] )
343+ deps_path = Path . join ( [ install_project_dir , "deps" ] )
344+
345+ assert File . ls! ( build_lib_path ) |> Enum . sort ( ) == [ "git_repo" , "mix_install" ]
346+ assert File . ls! ( deps_path ) == [ "git_repo" ]
347+
348+ System . put_env ( "MIX_INSTALL_RESTORE_PROJECT_DIR" , install_project_dir )
349+ end )
350+
351+ # Adding a dependency
352+
353+ with_cleanup ( fn ->
354+ Mix . install ( [
355+ { :git_repo , git: fixture_path ( "git_repo" ) } ,
356+ { :install_test , path: Path . join ( tmp_dir , "install_test" ) }
357+ ] )
358+
359+ assert_received { :mix_shell , :info , [ "==> install_test" ] }
360+ assert_received { :mix_shell , :info , [ "Compiling 2 files (.ex)" ] }
361+ assert_received { :mix_shell , :info , [ "Generated install_test app" ] }
362+ refute_received _
363+
364+ install_project_dir = Mix . install_project_dir ( )
365+ build_lib_path = Path . join ( [ install_project_dir , "_build" , "dev" , "lib" ] )
366+ deps_path = Path . join ( [ install_project_dir , "deps" ] )
367+
368+ assert File . ls! ( build_lib_path ) |> Enum . sort ( ) ==
369+ [ "git_repo" , "install_test" , "mix_install" ]
370+
371+ assert File . ls! ( deps_path ) == [ "git_repo" ]
372+
373+ System . put_env ( "MIX_INSTALL_RESTORE_PROJECT_DIR" , install_project_dir )
374+ end )
375+
376+ # Removing a dependency
377+
378+ with_cleanup ( fn ->
379+ Mix . install ( [
380+ { :install_test , path: Path . join ( tmp_dir , "install_test" ) }
381+ ] )
382+
383+ refute_received _
384+
385+ install_project_dir = Mix . install_project_dir ( )
386+ build_lib_path = Path . join ( [ install_project_dir , "_build" , "dev" , "lib" ] )
387+ deps_path = Path . join ( [ install_project_dir , "deps" ] )
388+
389+ assert File . ls! ( build_lib_path ) |> Enum . sort ( ) == [ "install_test" , "mix_install" ]
390+ assert File . ls! ( deps_path ) == [ ]
391+ end )
392+ after
393+ System . delete_env ( "MIX_INSTALL_RESTORE_PROJECT_DIR" )
394+ end
395+
338396 test "installed?" , % { tmp_dir: tmp_dir } do
339397 refute Mix . installed? ( )
340398
@@ -380,15 +438,7 @@ defmodule MixTest do
380438
381439 on_exit ( fn ->
382440 :code . set_path ( path )
383- purge ( [ InstallTest , InstallTest.MixProject , InstallTest.Protocol ] )
384-
385- ExUnit.CaptureLog . capture_log ( fn ->
386- Application . stop ( :git_repo )
387- Application . unload ( :git_repo )
388-
389- Application . stop ( :install_test )
390- Application . unload ( :install_test )
391- end )
441+ cleanup_deps ( )
392442 end )
393443
394444 Mix.State . put ( :installed , nil )
@@ -424,5 +474,37 @@ defmodule MixTest do
424474
425475 [ tmp_dir: tmp_dir ]
426476 end
477+
478+ defp with_cleanup ( fun ) do
479+ path = :code . get_path ( )
480+
481+ try do
482+ fun . ( )
483+ after
484+ :code . set_path ( path )
485+ cleanup_deps ( )
486+
487+ Mix.State . clear_cache ( )
488+ Mix.State . put ( :installed , nil )
489+ end
490+ end
491+
492+ defp cleanup_deps ( ) do
493+ purge ( [
494+ GitRepo ,
495+ GitRepo.MixProject ,
496+ InstallTest ,
497+ InstallTest.MixProject ,
498+ InstallTest.Protocol
499+ ] )
500+
501+ ExUnit.CaptureLog . capture_log ( fn ->
502+ Application . stop ( :git_repo )
503+ Application . unload ( :git_repo )
504+
505+ Application . stop ( :install_test )
506+ Application . unload ( :install_test )
507+ end )
508+ end
427509 end
428510end
0 commit comments