go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/luciexe/output_flag_test.go (about) 1 // Copyright 2019 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package luciexe 16 17 import ( 18 "flag" 19 "io" 20 "testing" 21 22 . "github.com/smartystreets/goconvey/convey" 23 . "go.chromium.org/luci/common/testing/assertions" 24 ) 25 26 func TestOutputFlag(t *testing.T) { 27 t.Parallel() 28 29 Convey(`OutputFlag`, t, func() { 30 fs := &flag.FlagSet{} 31 fs.SetOutput(io.Discard) 32 of := AddOutputFlagToSet(fs) 33 34 Convey(`good`, func() { 35 Convey(`empty`, func() { 36 So(fs.Parse(nil), ShouldBeNil) 37 So(of.Path, ShouldBeEmpty) 38 So(of.Codec, ShouldResemble, buildFileCodecNoop{}) 39 }) 40 41 Convey(`missing`, func() { 42 So(fs.Parse([]string{"other", "stuff"}), ShouldBeNil) 43 So(of.Path, ShouldBeEmpty) 44 So(of.Codec, ShouldResemble, buildFileCodecNoop{}) 45 }) 46 47 Convey(`equal`, func() { 48 So(fs.Parse([]string{OutputCLIArg + "=out.textpb", "a", "b"}), ShouldBeNil) 49 So(of.Path, ShouldResemble, "out.textpb") 50 So(of.Codec, ShouldResemble, buildFileCodecText{}) 51 So(fs.Args(), ShouldResemble, []string{"a", "b"}) 52 }) 53 54 Convey(`two val`, func() { 55 So(fs.Parse([]string{OutputCLIArg, "out.json", "a", "b"}), ShouldBeNil) 56 So(of.Path, ShouldResemble, "out.json") 57 So(of.Codec, ShouldResemble, buildFileCodecJSON{}) 58 So(fs.Args(), ShouldResemble, []string{"a", "b"}) 59 }) 60 61 Convey(`explicit noop`, func() { 62 So(fs.Parse([]string{OutputCLIArg + "="}), ShouldBeNil) 63 So(of.Path, ShouldBeEmpty) 64 So(of.Codec, ShouldResemble, buildFileCodecNoop{}) 65 }) 66 67 }) 68 69 Convey(`bad`, func() { 70 Convey(`incomplete`, func() { 71 So(fs.Parse([]string{OutputCLIArg}), ShouldErrLike, "flag needs an argument") 72 So(of.Path, ShouldBeEmpty) 73 So(of.Codec, ShouldResemble, buildFileCodecNoop{}) 74 }) 75 76 Convey(`bad extension`, func() { 77 So(fs.Parse([]string{OutputCLIArg, "nope", "arg"}), ShouldErrLike, "bad extension") 78 So(of.Path, ShouldBeEmpty) 79 So(of.Codec, ShouldResemble, buildFileCodecNoop{}) 80 }) 81 }) 82 }) 83 }