Skip to content

Commit 76a42cd

Browse files
authored
Merge pull request #81 from thaJeztah/mountinfo_simplify
mountinfo: GetMountsFromReader() remove redundant switch, fix dropped "optional" fields
2 parents 79f347c + df5b314 commit 76a42cd

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

mountinfo/mountinfo_linux.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,9 @@ func GetMountsFromReader(r io.Reader, filter FilterFunc) ([]*Info, error) {
105105
p.Options = fields[5]
106106

107107
// zero or more optional fields
108-
switch {
109-
case sepIdx == 6:
110-
// zero, do nothing
111-
case sepIdx == 7:
112-
p.Optional = fields[6]
113-
default:
114-
p.Optional = strings.Join(fields[6:sepIdx-1], " ")
115-
}
108+
p.Optional = strings.Join(fields[6:sepIdx], " ")
116109

117-
// Run the filter after parsing all of the fields.
110+
// Run the filter after parsing all fields.
118111
var skip, stop bool
119112
if filter != nil {
120113
skip, stop = filter(p)

mountinfo/mountinfo_linux_test.go

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -676,13 +676,13 @@ func TestParseMountinfoExtraCases(t *testing.T) {
676676
name: "one optional field",
677677
entry: `251 15 0:3573 / /mnt/point rw,relatime shared:123 - aufs none rw`,
678678
valid: true,
679-
exp: &Info{Mountpoint: "/mnt/point", FSType: "aufs", Source: "none", Optional: "shared:123 extra:tag what:ever"},
679+
exp: &Info{Mountpoint: "/mnt/point", FSType: "aufs", Source: "none", Optional: "shared:123"},
680680
},
681681
{
682682
name: "extra optional fields", // which we carefully gather
683683
entry: `251 15 0:3573 / /mnt/point rw,relatime shared:123 extra:tag what:ever key:value - aufs none rw`,
684684
valid: true,
685-
exp: &Info{Mountpoint: "/mnt/point", FSType: "aufs", Source: "none", Optional: "shared:123 extra:tag what:ever"},
685+
exp: &Info{Mountpoint: "/mnt/point", FSType: "aufs", Source: "none", Optional: "shared:123 extra:tag what:ever key:value"},
686686
},
687687
{
688688
name: "empty source field (kernel < 5.1 bug)",
@@ -693,34 +693,40 @@ func TestParseMountinfoExtraCases(t *testing.T) {
693693
}
694694

695695
for _, tc := range testcases {
696-
r := bytes.NewBufferString(tc.entry)
697-
info, err := GetMountsFromReader(r, nil)
698-
if !tc.valid {
699-
if err == nil {
700-
t.Errorf("case %q: expected error, got nil", tc.name)
696+
tc := tc
697+
t.Run(tc.name, func(t *testing.T) {
698+
r := bytes.NewBufferString(tc.entry)
699+
info, err := GetMountsFromReader(r, nil)
700+
if !tc.valid {
701+
if err == nil {
702+
t.Errorf("expected error, got nil")
703+
}
704+
return
701705
}
702-
continue
703-
}
704-
if err != nil {
705-
t.Errorf("case %q: expected no error, got %v", tc.name, err)
706-
continue
707-
}
708-
if len(info) != 1 {
709-
t.Errorf("case %q: expected 1 result, got %d", tc.name, len(info))
710-
}
711-
if tc.exp == nil {
712-
continue
713-
}
714-
i := info[0]
715-
if tc.exp.Mountpoint != "" && tc.exp.Mountpoint != i.Mountpoint {
716-
t.Errorf("case %q: expected mp %s, got %s", tc.name, tc.exp.Mountpoint, i.Mountpoint)
717-
}
718-
if tc.exp.FSType != "" && tc.exp.FSType != i.FSType {
719-
t.Errorf("case %q: expected fs %s, got %s", tc.name, tc.exp.FSType, i.FSType)
720-
}
721-
if tc.exp.Source != "" && tc.exp.Source != i.Source {
722-
t.Errorf("case %q: expected src %s, got %s", tc.name, tc.exp.Source, i.Source)
723-
}
706+
if err != nil {
707+
t.Errorf("expected no error, got %v", err)
708+
return
709+
}
710+
if len(info) != 1 {
711+
t.Errorf("expected 1 result, got %d", len(info))
712+
}
713+
if tc.exp == nil {
714+
return
715+
}
716+
i := info[0]
717+
if tc.exp.Mountpoint != "" && tc.exp.Mountpoint != i.Mountpoint {
718+
t.Errorf("expected mp %s, got %s", tc.exp.Mountpoint, i.Mountpoint)
719+
}
720+
if tc.exp.FSType != "" && tc.exp.FSType != i.FSType {
721+
t.Errorf("expected fs %s, got %s", tc.exp.FSType, i.FSType)
722+
}
723+
if tc.exp.Source != "" && tc.exp.Source != i.Source {
724+
t.Errorf("expected src %s, got %s", tc.exp.Source, i.Source)
725+
}
726+
if tc.exp.Optional != "" && tc.exp.Optional != i.Optional {
727+
t.Errorf("expected optional %s, got %s", tc.exp.Optional, i.Optional)
728+
}
729+
})
724730
}
725731
}
726732

0 commit comments

Comments
 (0)