Skip to content

Commit

Permalink
Add --check-locked to mix deps.get (#12184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Efesto committed Oct 10, 2022
1 parent 4a2747a commit b395381
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/mix/lib/mix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ defmodule Mix do
lockfile = Path.join(install_dir, "mix.lock")
old_lock = Mix.Dep.Lock.read(lockfile)
new_lock = Mix.Dep.Lock.read(external_lockfile)
Mix.Dep.Lock.write(lockfile, Map.merge(old_lock, new_lock))
Mix.Dep.Lock.write(Map.merge(old_lock, new_lock), file: lockfile)
File.write!(md5_path, Base.encode64(new_md5))
Mix.Task.rerun("deps.get")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/lib/mix/dep/fetcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ defmodule Mix.Dep.Fetcher do
# Merge the new lock on top of the old to guarantee we don't
# leave out things that could not be fetched and save it.
lock = Map.merge(old_lock, new_lock)
Mix.Dep.Lock.write(lock)
Mix.Dep.Lock.write(lock, opts)
mark_as_fetched(parent_deps)

# See if any of the deps diverged and abort.
Expand Down
12 changes: 10 additions & 2 deletions lib/mix/lib/mix/dep/lock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ defmodule Mix.Dep.Lock do
@doc """
Receives a map and writes it as the latest lock.
"""
@spec write(Path.t(), map()) :: :ok
def write(lockfile \\ lockfile(), map) do
@spec write(map(), keyword) :: :ok
def write(map, opts \\ []) do
lockfile = opts[:file] || lockfile()

unless map == read() do
if Keyword.get(opts, :check_locked, false) do
Mix.raise(
"Your #{lockfile} is out of date and must be updated without the --check-locked flag"
)
end

lines =
for {app, rev} <- Enum.sort(map), rev != nil do
~s( "#{app}": #{inspect(rev, limit: :infinity)},\n)
Expand Down
9 changes: 6 additions & 3 deletions lib/mix/lib/mix/tasks/deps.get.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ defmodule Mix.Tasks.Deps.Get do
## Command line options
* `--only` - only fetches dependencies for given environment
* `--check-locked` - raises if there are pending changes to the lockfile
* `--no-archives-check` - does not check archives before fetching deps
* `--only` - only fetches dependencies for given environment
"""

Expand All @@ -21,10 +22,12 @@ defmodule Mix.Tasks.Deps.Get do
end

Mix.Project.get!()
{opts, _, _} = OptionParser.parse(args, switches: [only: :string, target: :string])

{opts, _, _} =
OptionParser.parse(args, switches: [only: :string, target: :string, check_locked: :boolean])

fetch_opts =
for {switch, key} <- [only: :env, target: :target],
for {switch, key} <- [only: :env, target: :target, check_locked: :check_locked],
value = opts[switch],
do: {key, :"#{value}"}

Expand Down
14 changes: 14 additions & 0 deletions lib/mix/test/mix/dep/lock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ defmodule Mix.Dep.LockTest do
end)
end

test "raises a proper error if check_locked opt is true and there are changes", context do
in_tmp(context.test, fn ->
Mix.Dep.Lock.write(%{foo: :bar})

Mix.Dep.Lock.write(%{foo: :bar}, check_locked: true)

assert_raise Mix.Error,
~r/Your mix\.lock is out of date and must be updated without the --check-locked flag/,
fn ->
Mix.Dep.Lock.write(%{foo: :bar, bar: :bat}, check_locked: true)
end
end)
end

test "raises a proper error for merge conflicts", context do
in_tmp(context.test, fn ->
File.write("mix.lock", ~S"""
Expand Down
4 changes: 2 additions & 2 deletions lib/mix/test/mix_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ defmodule MixTest do
Mix.Project.push(GitApp)
[_latest_rev, rev | _] = get_git_repo_revs("git_repo")
lockfile = Path.join(tmp_dir, "lock")
Mix.Dep.Lock.write(lockfile, %{git_repo: {:git, fixture_path("git_repo"), rev, []}})
Mix.Dep.Lock.write(%{git_repo: {:git, fixture_path("git_repo"), rev, []}}, file: lockfile)
Mix.ProjectStack.pop()

Mix.install(
Expand Down Expand Up @@ -256,7 +256,7 @@ defmodule MixTest do

Mix.Project.push(GitApp)
lockfile = Path.join(tmp_dir, "lock")
Mix.Dep.Lock.write(lockfile, %{git_repo: {:git, fixture_path("git_repo"), rev2, []}})
Mix.Dep.Lock.write(%{git_repo: {:git, fixture_path("git_repo"), rev2, []}}, file: lockfile)
Mix.ProjectStack.pop()

Mix.install(
Expand Down

0 comments on commit b395381

Please sign in to comment.