# Check that 'go get' adds sums for updated modules if we had sums before,
# even if we didn't load packages from them.
# Verifies #44129.

env fmt='{{.ImportPath}}: {{if .Error}}{{.Error.Err}}{{else}}ok{{end}}'

# Control case: before upgrading, we have the sums we need.
# go list -deps -e -f $fmt .
# stdout '^rsc.io/quote: ok$'
# ! stdout rsc.io/sampler  # not imported by quote in this version
cp go.mod.orig go.mod
cp go.sum.orig go.sum
go mod tidy
cmp go.mod.orig go.mod
cmp go.sum.orig go.sum


# Upgrade a module. This also upgrades rsc.io/quote, and though we didn't load
# a package from it, we had the sum for its old version, so we need the
# sum for the new version, too.
go get example.com/upgrade@v0.0.2
grep '^rsc.io/quote v1.5.2 ' go.sum

# The upgrade still breaks the build because the new version of quote imports
# rsc.io/sampler, and we don't have its zip sum.
go list -deps -e -f $fmt
stdout 'rsc.io/quote: ok'
stdout 'rsc.io/sampler: missing go.sum entry for module providing package rsc.io/sampler'
cp go.mod.orig go.mod
cp go.sum.orig go.sum


# Replace the old version with a directory before upgrading.
# We didn't need a sum for it before (even though we had one), so we won't
# fetch a new sum.
go mod edit -replace rsc.io/quote@v1.0.0=./dummy
go get example.com/upgrade@v0.0.2
! grep '^rsc.io/quote v1.5.2 ' go.sum
cp go.mod.orig go.mod
cp go.sum.orig go.sum


# Replace the new version with a directory before upgrading.
# We can't get a sum for a directory.
go mod edit -replace rsc.io/quote@v1.5.2=./dummy
go get example.com/upgrade@v0.0.2
! grep '^rsc.io/quote v1.5.2 ' go.sum
cp go.mod.orig go.mod
cp go.sum.orig go.sum


# Replace the new version with a different version.
# We should get a sum for that version.
go mod edit -replace rsc.io/quote@v1.5.2=rsc.io/quote@v1.5.1
go get example.com/upgrade@v0.0.2
! grep '^rsc.io/quote v1.5.2 ' go.sum
grep '^rsc.io/quote v1.5.1 ' go.sum
cp go.mod.orig go.mod
cp go.sum.orig go.sum


# Delete the new version's zip (but not mod) from the cache and go offline.
# 'go get' should fail when fetching the zip.
rm $GOPATH/pkg/mod/cache/download/rsc.io/quote/@v/v1.5.2.zip
env GOPROXY=off
! go get example.com/upgrade@v0.0.2
stderr '^go: upgraded rsc.io/quote v1.0.0 => v1.5.2: error finding sum for rsc.io/quote@v1.5.2: module lookup disabled by GOPROXY=off$'

-- go.mod.orig --
module m

go 1.16

require (
	example.com/upgrade v0.0.1
	rsc.io/quote v1.0.0
)

replace (
	example.com/upgrade v0.0.1 => ./upgrade1
	example.com/upgrade v0.0.2 => ./upgrade2
)
-- go.sum.orig --
rsc.io/quote v1.0.0 h1:kQ3IZQzPTiDJxSZI98YaWgxFEhlNdYASHvh+MplbViw=
rsc.io/quote v1.0.0/go.mod h1:v83Ri/njykPcgJltBc/gEkJTmjTsNgtO1Y7vyIK1CQA=
-- go.sum.want --
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
rsc.io/quote v1.0.0 h1:kQ3IZQzPTiDJxSZI98YaWgxFEhlNdYASHvh+MplbViw=
rsc.io/quote v1.0.0/go.mod h1:v83Ri/njykPcgJltBc/gEkJTmjTsNgtO1Y7vyIK1CQA=
rsc.io/quote v1.5.2 h1:3fEykkD9k7lYzXqCYrwGAf7iNhbk4yCjHmKBN9td4L0=
rsc.io/quote v1.5.2/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
-- use.go --
package use

import (
	_ "example.com/upgrade"
	_ "rsc.io/quote"
)
-- upgrade1/go.mod --
module example.com/upgrade

go 1.16
-- upgrade1/upgrade.go --
package upgrade
-- upgrade2/go.mod --
module example.com/upgrade

go 1.16

require rsc.io/quote v1.5.2 // indirect
-- upgrade2/upgrade.go --
package upgrade
-- dummy/go.mod --
module rsc.io/quote

go 1.16
-- dummy/quote.go --
package quote

