Control deprecation warnings with a number instead of a bool.

Previously, to avoid showing deprecation warnings for the next release when the
user is running on HEAD, a boolean CLI flag -show-deprecations is introduced,
and is set to false in the master branch. The idea is that release branches will
have this default to true, so people running released versions will see
deprecations.

However, this means that people running on HEAD will never see any deprecations
unless they use this CLI flag, which is not ideal. This commit replaces the flag
bool -show-deprecations with a numerical -deprecation-level flag, which requests
deprecations that are active as of release 0.X to be shown. The default value of
this flag will be the minor version number of the *last* release, so that people
running HEAD will see as many deprecation warnings as people running the last
release would. This number will be bumped just before releases.
This commit is contained in:
Qi Xiao 2021-01-19 21:37:36 +00:00
parent 87519a9d9c
commit 47d9766f5c
8 changed files with 28 additions and 25 deletions

View File

@ -7,7 +7,6 @@ import (
"github.com/elves/elvish/pkg/eval"
. "github.com/elves/elvish/pkg/eval/evaltest"
"github.com/elves/elvish/pkg/eval/vals"
"github.com/elves/elvish/pkg/prog"
"github.com/elves/elvish/pkg/testutil"
)
@ -63,9 +62,6 @@ func TestCompleteFilename(t *testing.T) {
}
func TestComplexCandidate(t *testing.T) {
restore := prog.SetShowDeprecations(true)
defer restore()
TestWithSetup(t, func(ev *eval.Evaler) {
ev.AddGlobal(eval.NsBuilder{}.AddGoFn("", "cc", complexCandidate).Ns())
},

View File

@ -273,7 +273,7 @@ func TestUse(t *testing.T) {
// Regression test for #1072
func TestUse_WarnsAboutDeprecatedFeatures(t *testing.T) {
restore := prog.SetShowDeprecations(true)
restore := prog.SetDeprecationLevel(15)
defer restore()
libdir, cleanup := testutil.InTestDir()
defer cleanup()

View File

@ -245,9 +245,10 @@ func (op *indexingOp) exec(fm *Frame) ([]interface{}, Exception) {
if err != nil {
return nil, fm.errorp(op, err)
}
// Check the legacy low:high slice syntax deprecated since 0.15.
deprecation := vals.CheckDeprecatedIndex(v, index)
if deprecation != "" {
fm.Deprecate(deprecation, diag.NewContext(fm.srcMeta.Name, fm.srcMeta.Code, indexOp))
fm.Deprecate(deprecation, indexOp, 15)
}
newvs = append(newvs, result)
}

View File

@ -41,14 +41,14 @@ func TestArgs(t *testing.T) {
}
func TestEvalTimeDeprecate(t *testing.T) {
restore := prog.SetShowDeprecations(true)
restore := prog.SetDeprecationLevel(42)
defer restore()
_, cleanup := testutil.InTestDir()
defer cleanup()
TestWithSetup(t, func(ev *Evaler) {
ev.AddGlobal(NsBuilder{}.AddGoFn("", "dep", func(fm *Frame) {
fm.Deprecate("deprecated", nil)
fm.Deprecate("deprecated", nil, 42)
}).Ns())
},
That("dep").PrintsStderrWith("deprecated"),
@ -58,7 +58,7 @@ func TestEvalTimeDeprecate(t *testing.T) {
}
func TestCompileTimeDeprecation(t *testing.T) {
restore := prog.SetShowDeprecations(true)
restore := prog.SetDeprecationLevel(15)
defer restore()
ev := NewEvaler()

View File

@ -209,12 +209,15 @@ func (fm *Frame) errorpf(r diag.Ranger, format string, args ...interface{}) Exce
// Deprecate shows a deprecation message. The message is not shown if the same
// deprecation message has been shown for the same location before.
func (fm *Frame) Deprecate(msg string, ctx *diag.Context) {
if ctx == nil {
func (fm *Frame) Deprecate(msg string, r diag.Ranger, minLevel int) {
var ctx *diag.Context
if r == nil {
ctx = fm.traceback.Head
} else {
ctx = diag.NewContext(fm.srcMeta.Name, fm.srcMeta.Code, r)
}
dep := deprecation{ctx.Name, ctx.Ranging, msg}
if prog.ShowDeprecations && fm.Evaler.registerDeprecation(dep) {
if prog.DeprecationLevel >= minLevel && fm.Evaler.registerDeprecation(dep) {
err := diag.Error{
Type: "deprecation", Message: dep.message, Context: *ctx}
fm.ErrorFile().WriteString(err.Show("") + "\n")

View File

@ -192,6 +192,7 @@ func (cp *compiler) checkDeprecatedBuiltin(name string, r diag.Ranger) {
return
}
msg := ""
minLevel := 15
switch name {
case "-source~":
msg = `the "source" command is deprecated; use "eval" instead`
@ -223,7 +224,7 @@ func (cp *compiler) checkDeprecatedBuiltin(name string, r diag.Ranger) {
return
}
dep := deprecation{cp.srcMeta.Name, r.Range(), msg}
if prog.ShowDeprecations && cp.deprecations.register(dep) {
if prog.DeprecationLevel >= minLevel && cp.deprecations.register(dep) {
err := diag.Error{
Type: "deprecation", Message: msg,
Context: diag.Context{

View File

@ -20,15 +20,17 @@ import (
// resembles "elvi".
const defaultWebPort = 3171
// ShowDeprecations is a global flag that controls whether to show deprecations.
var ShowDeprecations = false
// DeprecationLevel is a global flag that controls which deprecations to show.
// If its value is X, Elvish shows deprecations that should be shown for version
// 0.X.
var DeprecationLevel = 14
// SetShowDeprecations sets ShowDeprecations to the given value, and returns a
// SetDeprecationLevel sets ShowDeprecations to the given value, and returns a
// function to restore the old value.
func SetShowDeprecations(b bool) func() {
save := ShowDeprecations
ShowDeprecations = b
return func() { ShowDeprecations = save }
func SetDeprecationLevel(level int) func() {
save := DeprecationLevel
DeprecationLevel = level
return func() { DeprecationLevel = save }
}
// Flags keeps command-line flags.
@ -74,7 +76,7 @@ func newFlagSet(stderr io.Writer, f *Flags) *flag.FlagSet {
fs.StringVar(&f.DB, "db", "", "path to the database")
fs.StringVar(&f.Sock, "sock", "", "path to the daemon socket")
fs.BoolVar(&ShowDeprecations, "show-deprecations", ShowDeprecations, "whether to show deprecations")
fs.IntVar(&DeprecationLevel, "deprecation-level", DeprecationLevel, "show warnings for all features deprecated as of version 0.X")
return fs
}

View File

@ -50,14 +50,14 @@ func TestHelp(t *testing.T) {
}
func TestShowDeprecations(t *testing.T) {
restore := SetShowDeprecations(false)
restore := SetDeprecationLevel(0)
defer restore()
f := Setup()
defer f.Cleanup()
Run(f.Fds(), Elvish("-show-deprecations"), testProgram{shouldRun: true})
if !ShowDeprecations {
t.Errorf("ShowDeprecations = false, want true")
Run(f.Fds(), Elvish("-deprecation-level", "42"), testProgram{shouldRun: true})
if DeprecationLevel != 42 {
t.Errorf("ShowDeprecations = %d, want 42", DeprecationLevel)
}
}