Skip to content

Commit

Permalink
wrappers/desktop: add more context to logs for malformed desktop files
Browse files Browse the repository at this point in the history
Signed-off-by: Zeyad Gouda <[email protected]>
  • Loading branch information
ZeyadYasser authored and pedronis committed Dec 20, 2024
1 parent 08d3c8d commit bc60ea4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 6 additions & 2 deletions wrappers/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,13 @@ func forAllDesktopFiles(cb func(base, instanceName string) error) error {
}

de, err := desktopentry.Read(desktopFile)
if err != nil || de.SnapInstanceName == "" {
if err != nil {
// cannot read instance name from desktop file, ignore
logger.Noticef("cannot read instance name: %s", err)
logger.Noticef("cannot read instance name from %q: %v", desktopFile, err)
continue
}
if de.SnapInstanceName == "" {
logger.Noticef("cannot find X-SnapInstanceName entry in %q", desktopFile)
continue
}

Expand Down
22 changes: 22 additions & 0 deletions wrappers/desktop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package wrappers_test

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -882,3 +883,24 @@ X-SnapInstanceName=foo`)
c.Check(found["foo_foo.desktop"], Equals, "foo")
c.Check(found["foo_bar.desktop"], Equals, "foo")
}

func (s *desktopSuite) TestForAllDesktopFilesSkipsBadDesktopFiles(c *C) {
c.Assert(os.MkdirAll(dirs.SnapDesktopFilesDir, 0755), IsNil)

var mockEmpty []byte
var mockNoInstanceName = []byte(`
[Desktop Entry]
Name=foo`)
var mockInvalid = []byte(`
[Desktop Entry]
[Desktop Entry]`)

c.Assert(os.WriteFile(filepath.Join(dirs.SnapDesktopFilesDir, "empty.desktop"), mockEmpty, 0644), IsNil)
c.Assert(os.WriteFile(filepath.Join(dirs.SnapDesktopFilesDir, "no-instance-name.desktop"), mockNoInstanceName, 0644), IsNil)
c.Assert(os.WriteFile(filepath.Join(dirs.SnapDesktopFilesDir, "invalid.desktop"), mockInvalid, 0644), IsNil)

err := wrappers.ForAllDesktopFiles(func(base, instanceName string) error {
return errors.New("unexpected call")
})
c.Assert(err, IsNil)
}

0 comments on commit bc60ea4

Please sign in to comment.