Skip to content

Commit a57c250

Browse files
author
Scott Moser
authored
Adjust code for less ignoring gomnd. (project-machine#94)
Disable the gomnd (magic number) linter on test files. Adjust code to use consts in most cases where we were ignoring the linter (// nolint:gomnd).
1 parent ea0568a commit a57c250

15 files changed

+70
-58
lines changed

.golangci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ issues:
1212
# 1 is not a magic number. 3 is a magic number.
1313
- linters: ["gomnd"]
1414
text: "Magic number: 1,"
15-
# test files can have globals.
16-
- linters: ["gochecknoglobals"]
15+
# test files can have globals and magic numbers.
16+
- linters: ["gochecknoglobals", "gomnd"]
1717
path: _test.go

demo/misc.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func pathExists(fpath string) bool {
7676
}
7777

7878
func findPartInfo(diskPath string) (disko.Partition, error) {
79-
const mysize = 200 * disko.Mebibyte //nolint: gomnd
79+
const mysize = 200 * disko.Mebibyte
8080
var err error
8181

8282
mysys := linux.System()
@@ -142,7 +142,7 @@ func miscUpDown(c *cli.Context) error {
142142
var vg disko.VG
143143
var lv disko.LV
144144

145-
var createMiB, extendMiB uint64 = 100, 48 //nolint: gomnd
145+
var createMiB, extendMiB uint64 = 100, 48
146146

147147
if fname == "" {
148148
return fmt.Errorf("must provide disk/file to partition")

disk_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,29 @@ func TestPartitionSize(t *testing.T) {
4747

4848
func TestDiskString(t *testing.T) {
4949
mib := disko.Mebibyte
50-
gb := uint64(1000 * 1000 * 1000) // nolint: gomnd
50+
gb := uint64(1000 * 1000 * 1000)
5151

5252
d := disko.Disk{
5353
Name: "sde",
5454
Path: "/dev/sde",
5555
Size: gb,
56-
SectorSize: 512, //nolint: gomnd
56+
SectorSize: 512,
5757
Type: disko.HDD,
5858
Attachment: disko.ATA,
5959
Partitions: disko.PartitionSet{
60-
1: {Start: 3 * mib, Last: 253*mib - 1, Number: 1}, //nolint: gomnd
61-
3: {Start: 500 * mib, Last: 600*mib - 1, Number: 3}, //nolint: gomnd
60+
1: {Start: 3 * mib, Last: 253*mib - 1, Number: 1},
61+
3: {Start: 500 * mib, Last: 600*mib - 1, Number: 3},
6262
},
6363
UdevInfo: disko.UdevInfo{},
6464
}
6565
found := " " + d.String() + " "
6666

6767
// disk size 1gb = 953 MiB. 600 = (253-3) + (953-600)
68-
expectedFree := 600 // nolint: gomnd
68+
expectedFree := 600
6969

7070
for _, substr := range []string{
7171
fmt.Sprintf("Size=%d", gb),
72-
fmt.Sprintf("FreeSpace=%dMiB/2", expectedFree), //nolint: gomnd
72+
fmt.Sprintf("FreeSpace=%dMiB/2", expectedFree),
7373
fmt.Sprintf("NumParts=%d", len(d.Partitions))} {
7474
if !strings.Contains(found, " "+substr+" ") {
7575
t.Errorf("%s: missing expected substring ' %s '", found, substr)
@@ -83,11 +83,11 @@ func TestDiskDetails(t *testing.T) {
8383
Name: "sde",
8484
Path: "/dev/sde",
8585
Size: mib * mib,
86-
SectorSize: 512, //nolint: gomnd
86+
SectorSize: 512,
8787
Type: disko.HDD,
8888
Attachment: disko.ATA,
8989
Partitions: disko.PartitionSet{
90-
1: {Start: 3 * mib, Last: 253*mib - 1, Number: 1}, //nolint: gomnd
90+
1: {Start: 3 * mib, Last: 253*mib - 1, Number: 1},
9191
},
9292
UdevInfo: disko.UdevInfo{},
9393
}
@@ -152,7 +152,7 @@ func TestPartitionSerializeJson(t *testing.T) {
152152
myIDStr := "01234567-89AB-CDEF-0123-456789ABCDEF"
153153
myID, _ := disko.StringToGUID(myIDStr)
154154
p := disko.Partition{
155-
Start: 3 * disko.Mebibyte, //nolint:gomnd
155+
Start: 3 * disko.Mebibyte,
156156
Last: 253*disko.Mebibyte - 1,
157157
ID: myID,
158158
Type: partid.EFI,
@@ -198,8 +198,8 @@ func TestPartitionUnserializeJson(t *testing.T) {
198198
}
199199

200200
expected := disko.Partition{
201-
Start: 3 * disko.Mebibyte, // nolint:gomnd
202-
Last: 253*disko.Mebibyte - 1, // nolint:gomnd
201+
Start: 3 * disko.Mebibyte,
202+
Last: 253*disko.Mebibyte - 1,
203203
ID: myID,
204204
Type: partid.EFI,
205205
Name: "my system part",
@@ -217,8 +217,8 @@ func TestDiskSerializeJson(t *testing.T) {
217217
d := disko.Disk{
218218
Name: "sda",
219219
Path: "/dev/sda",
220-
Size: 500 * disko.Mebibyte, //nolint:gomnd
221-
SectorSize: 512, //nolint:gomnd
220+
Size: 500 * disko.Mebibyte,
221+
SectorSize: 512,
222222
Type: disko.HDD,
223223
Attachment: disko.ATA,
224224
}
@@ -247,8 +247,8 @@ func TestDiskUnserializeJson(t *testing.T) {
247247
expected := disko.Disk{
248248
Name: "sda",
249249
Path: "/dev/sda",
250-
Size: 500 * disko.Mebibyte, //nolint:gomnd
251-
SectorSize: 512, //nolint:gomnd
250+
Size: 500 * disko.Mebibyte,
251+
SectorSize: 512,
252252
Type: disko.HDD,
253253
Attachment: disko.ATA,
254254
}

linux/disk.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
const (
2525
sectorSize512 = 512
2626
sectorSize4k = 4096
27+
max32 = 0xFFFFFFFF
2728
)
2829

2930
// ErrNoPartitionTable is returned if there is no partition table.
@@ -319,7 +320,7 @@ func getPartName(s string) [72]byte {
319320

320321
for i, r := range codes {
321322
b[i*2] = byte(r)
322-
b[i*2+1] = byte(r >> 8) //nolint:gomnd
323+
b[i*2+1] = byte(r >> 8) // nolint:gomnd
323324
}
324325

325326
return b
@@ -446,20 +447,23 @@ func addPartitionSetMBR(fp io.ReadWriteSeeker, d disko.Disk, pSet disko.Partitio
446447

447448
func rangeCheckParts(d disko.Disk, pSet disko.PartitionSet) error {
448449
maxSize := d.Size
450+
449451
if d.Table == disko.MBR {
450-
maxSize = uint64(0xFFFFFFFF) * uint64(d.SectorSize) //nolint: gomnd
452+
maxSize = uint64(max32) * uint64(d.SectorSize)
451453
}
452454

453455
maxEnd := ((maxSize - uint64(d.SectorSize)*33) / disko.Mebibyte) * disko.Mebibyte
454456
minStart := disko.Mebibyte
455457

456-
minPartNum, maxPartNum := uint(1), uint(128) //nolint: gomnd
458+
const minPartNum, maxPartNumMBR, maxPartNumGPT = 1, 4, 128
459+
460+
maxPartNum := uint(maxPartNumGPT)
457461
if d.Table == disko.MBR {
458-
maxPartNum = 4
462+
maxPartNum = uint(maxPartNumMBR)
459463
}
460464

461465
for _, p := range pSet {
462-
if p.Number < minPartNum || p.Number > maxPartNum {
466+
if p.Number < uint(minPartNum) || p.Number > maxPartNum {
463467
return fmt.Errorf("partition number %d is out of range (%d-%d) for %s",
464468
p.Number, minPartNum, maxPartNum, d.Table)
465469
}
@@ -825,7 +829,7 @@ func newProtectiveMBR(buf []byte, sectorSize uint, diskSize uint64) (mbr.MBR, er
825829
// length as 0xFFFFFFFF which is what windows and sfdisk do.
826830
// sfdisk actually complains about our -1 value.
827831
// see https://github.com/rekby/mbr/pull/2/files
828-
max := uint64(0xFFFFFFFF) // nolint: gomnd
832+
max := uint64(max32)
829833
if diskSize/uint64(sectorSize) > max {
830834
pt.SetLBALen(uint32(max) - 1)
831835
} else {

linux/disk_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func TestMyPartition(t *testing.T) {
169169
defer os.RemoveAll(tmpd)
170170

171171
fpath := path.Join(tmpd, "mydisk")
172-
fsize := uint64(200 * 1024 * 1024) // nolint:gomnd
172+
fsize := uint64(200 * 1024 * 1024)
173173

174174
if err := ioutil.WriteFile(fpath, []byte{}, 0644); err != nil {
175175
t.Fatalf("Failed to write to a temp file: %s", err)
@@ -243,7 +243,7 @@ func TestMyPartitionMBR(t *testing.T) {
243243
defer os.RemoveAll(tmpd)
244244

245245
fpath := path.Join(tmpd, "mydisk")
246-
fsize := uint64(200 * 1024 * 1024) // nolint:gomnd
246+
fsize := uint64(200 * 1024 * 1024)
247247

248248
if err := ioutil.WriteFile(fpath, []byte{}, 0644); err != nil {
249249
t.Fatalf("Failed to write to a temp file: %s", err)
@@ -308,11 +308,11 @@ func TestWipeDisk(t *testing.T) {
308308
t.Fatalf("Failed to create tempdir: %s", err)
309309
}
310310

311-
mib := uint64(1024 * 1024) // nolint: gomnd
311+
mib := uint64(1024 * 1024)
312312

313313
defer os.RemoveAll(tmpd)
314314

315-
disk, err := genTempGptDisk(tmpd, 50*mib) // nolint:gomnd
315+
disk, err := genTempGptDisk(tmpd, 50*mib)
316316
if err != nil {
317317
t.Fatalf("Creation of temp disk failed: %s", err)
318318
}
@@ -402,7 +402,7 @@ func TestDeletePartition(t *testing.T) {
402402

403403
defer os.RemoveAll(tmpd)
404404

405-
disk, err := genTempGptDisk(tmpd, 200*1024*1024) // nolint:gomnd
405+
disk, err := genTempGptDisk(tmpd, 200*1024*1024)
406406
if err != nil {
407407
t.Fatalf("Creation of temp disk failed: %s", err)
408408
}
@@ -445,7 +445,7 @@ func TestBadPartition(t *testing.T) {
445445
defer os.RemoveAll(tmpd)
446446

447447
fpath := path.Join(tmpd, "mydisk")
448-
fsize := uint64(200 * 1024 * 1024) // nolint:gomnd
448+
fsize := uint64(200 * 1024 * 1024)
449449

450450
if err := ioutil.WriteFile(fpath, []byte{}, 0644); err != nil {
451451
t.Fatalf("Failed to write to a temp file: %s", err)
@@ -466,7 +466,7 @@ func TestBadPartition(t *testing.T) {
466466
myGUID := disko.GenGUID()
467467

468468
part := disko.Partition{
469-
Start: 1024, // nolint:gomnd
469+
Start: 1024,
470470
Last: fs[0].Last,
471471
Type: partid.LinuxLVM,
472472
Name: "mytest partition",

linux/lvm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (ls *linuxLVM) HasPV(name string) bool {
201201
}
202202

203203
func (ls *linuxLVM) CreateVG(name string, pvs ...disko.PV) (disko.VG, error) {
204-
mdSize := 128 * disko.Mebibyte // nolint:gomnd
204+
const mdSize = 128 * disko.Mebibyte
205205
cmd := []string{"lvm", "vgcreate",
206206
fmt.Sprintf("--metadatasize=%dB", mdSize),
207207
"--zero=y", name}

linux/root_helpers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ func waitForFileSize(devPath string) error {
132132
defer fp.Close()
133133

134134
diskLen := int64(0)
135-
napLen := time.Millisecond * 10 //nolint: gomnd
135+
napLen := time.Millisecond * 10
136136
startTime := time.Now()
137-
endTime := startTime.Add(30 * time.Second) // nolint: gomnd
137+
endTime := startTime.Add(30 * time.Second)
138138

139139
for {
140140
if diskLen, err = fp.Seek(0, io.SeekEnd); err != nil {

linux/root_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// +build linux,!skipIntegration
22

3-
// nolint:errcheck,gomnd,funlen
3+
// nolint:errcheck,funlen
44
package linux_test
55

66
import (

linux/util_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -134,24 +134,24 @@ func TestRunCommand(t *testing.T) {
134134

135135
func TestCeilingUp(t *testing.T) {
136136
assert := assert.New(t)
137-
assert.Equal(uint64(100), Ceiling(98, 4)) //nolint: gomnd
137+
assert.Equal(uint64(100), Ceiling(98, 4))
138138
}
139139

140140
func TestCeilingEven(t *testing.T) {
141141
assert := assert.New(t)
142-
assert.Equal(uint64(100), Ceiling(100, 4)) //nolint: gomnd
143-
assert.Equal(uint64(97), Ceiling(97, 1)) //nolint: gomnd
142+
assert.Equal(uint64(100), Ceiling(100, 4))
143+
assert.Equal(uint64(97), Ceiling(97, 1))
144144
}
145145

146146
func TestFloorDown(t *testing.T) {
147147
assert := assert.New(t)
148-
assert.Equal(uint64(96), Floor(98, 4)) //nolint: gomnd
148+
assert.Equal(uint64(96), Floor(98, 4))
149149
}
150150

151151
func TestFloorEven(t *testing.T) {
152152
assert := assert.New(t)
153-
assert.Equal(uint64(100), Floor(100, 4)) //nolint: gomnd
154-
assert.Equal(uint64(97), Floor(97, 1)) //nolint: gomnd
153+
assert.Equal(uint64(100), Floor(100, 4))
154+
assert.Equal(uint64(97), Floor(97, 1))
155155
}
156156

157157
func TestGetFileSize(t *testing.T) {

megaraid/storcli.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,15 @@ func loadSections(cmdOut string) []scResultSection {
192192

193193
func parseKeyValData(lines []string) map[string]string {
194194
data := map[string]string{}
195+
const tokNum2 = 2
195196

196197
for _, line := range lines {
197198
if line == "" {
198199
continue
199200
}
200201

201-
toks := strings.SplitN(line, " = ", 2)
202-
if len(toks) != 2 { // nolint:gomnd
202+
toks := strings.SplitN(line, " = ", tokNum2)
203+
if len(toks) != tokNum2 {
203204
continue
204205
}
205206

@@ -380,6 +381,7 @@ func parseCxDallShow(cmdOut string) (VirtDriveSet, DriveSet, error) {
380381
func parseVirtProperties(cmdOut string) (map[int](map[string]string), error) {
381382
var vID int
382383
var err error
384+
const tokNum2 = 2
383385

384386
nameMatch := regexp.MustCompile("^VD([0-9]+) Properties$")
385387
vdmap := map[int](map[string]string){}
@@ -403,7 +405,7 @@ func parseVirtProperties(cmdOut string) (map[int](map[string]string), error) {
403405
// Extract the VirtDrive Number from the Name (VD0 Properties)
404406
toks := nameMatch.FindStringSubmatch(sect.Name)
405407

406-
if len(toks) != 2 { // nolint: gomnd
408+
if len(toks) != tokNum2 {
407409
return vdmap, fmt.Errorf("failed parsing section '%s'", sect.Name)
408410
}
409411

@@ -468,6 +470,7 @@ func parseDriveGroupVal(val string) (int, error) {
468470
func pdDataToDrive(data map[string]string) (Drive, error) {
469471
var err error
470472
var dID, dg, eID, slot int
473+
const tokNum2 = 2
471474

472475
if dID, err = parseIntOrDash(data["DID"]); err != nil {
473476
return Drive{}, err
@@ -477,8 +480,8 @@ func pdDataToDrive(data map[string]string) (Drive, error) {
477480
return Drive{}, err
478481
}
479482

480-
toks := strings.SplitN(data["EID:Slt"], ":", 2)
481-
if len(toks) != 2 { // nolint:gomnd
483+
toks := strings.SplitN(data["EID:Slt"], ":", tokNum2)
484+
if len(toks) != tokNum2 {
482485
return Drive{},
483486
fmt.Errorf(
484487
"splitting EID:Slt data '%s' on ':'' returned %d fields, expected 2",
@@ -560,9 +563,11 @@ type cachingStorCli struct {
560563

561564
// CachingStorCli - just a cache for a MegaRaid
562565
func CachingStorCli() MegaRaid {
566+
const longTime = 5 * time.Minute
567+
563568
return &cachingStorCli{
564569
mr: &storCli{},
565-
cache: cache.New(5*time.Minute, 5*time.Minute), //nolint: gomnd
570+
cache: cache.New(longTime, longTime),
566571
}
567572
}
568573

megaraid/storcli_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ func TestParseCxDallShowNotConfigured(t *testing.T) {
110110
t.Errorf("Expected %d pds found %d\n", exPlen, len(pds))
111111
}
112112

113-
// nolint:gomnd
114113
expected := DriveSet{
115114
13: &Drive{ID: 13, EID: 62, Slot: 3, DriveGroup: -1,
116115
MediaType: HDD, Model: "MZ6ER400HAGL/003", State: "UBUnsp"},
@@ -268,7 +267,6 @@ func TestNewController(t *testing.T) {
268267
}
269268

270269
func TestJsonDriveGroupSet(t *testing.T) {
271-
//nolint: gomnd
272270
dgs := DriveGroupSet{
273271
1: &DriveGroup{
274272
ID: 1,

mockos/lvm_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func TestVG(t *testing.T) {
143143
})
144144
}
145145

146-
//nolint: gomnd,funlen
146+
//nolint: funlen
147147
func TestLV(t *testing.T) {
148148
Convey("test lvm lvs", t, func() {
149149
sys := mockos.System("testdata/model_sys.json")

mockos/system_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
. "github.com/smartystreets/goconvey/convey"
1010
)
1111

12-
//nolint: funlen, gomnd
12+
//nolint: funlen
1313
func TestSystem(t *testing.T) {
1414
myID, _ := disko.StringToGUID("01234567-89AB-CDEF-0123-456789ABCDEF")
1515

0 commit comments

Comments
 (0)