How to use Open method of launcher Package

Best Rod code snippet using launcher.Open

launcher_test.go

Source:launcher_test.go Github

copy

Full Screen

...33func Test(t *testing.T) { TestingT(t) }34type launcherSuite struct {35 testutil.BaseTest36 launcher *userd.Launcher37 mockXdgOpen *testutil.MockCmd38 mockXdgMime *testutil.MockCmd39}40var _ = Suite(&launcherSuite{})41func (s *launcherSuite) SetUpTest(c *C) {42 s.BaseTest.SetUpTest(c)43 s.AddCleanup(release.MockOnClassic(true))44 s.launcher = &userd.Launcher{}45 s.mockXdgOpen = testutil.MockCommand(c, "xdg-open", "")46 s.AddCleanup(s.mockXdgOpen.Restore)47 s.mockXdgMime = testutil.MockCommand(c, "xdg-mime", "")48 s.AddCleanup(s.mockXdgMime.Restore)49 s.AddCleanup(userd.MockSnapFromSender(func(*dbus.Conn, dbus.Sender) (string, error) {50 return "some-snap", nil51 }))52}53func (s *launcherSuite) TestOpenURLWithNotAllowedScheme(c *C) {54 for _, t := range []struct {55 url string56 errMatcher string57 scheme string58 }{59 {"tel://049112233445566", `Supplied URL scheme "tel" is not allowed`, "tel"},60 {"aabbccdd0011", "cannot open URL without a scheme", ""},61 {"invälid:%url", dbus.ErrMsgInvalidArg.Error(), ""},62 } {63 s.mockXdgMime.ForgetCalls()64 err := s.launcher.OpenURL(t.url, ":some-dbus-sender")65 c.Assert(err, ErrorMatches, t.errMatcher)66 c.Assert(s.mockXdgOpen.Calls(), IsNil)67 if t.scheme != "" {68 c.Assert(s.mockXdgMime.Calls(), DeepEquals, [][]string{69 {"xdg-mime", "query", "default", "x-scheme-handler/" + t.scheme},70 })71 } else {72 c.Assert(s.mockXdgMime.Calls(), IsNil)73 }74 }75}76func (s *launcherSuite) TestOpenURLWithAllowedSchemeHappy(c *C) {77 for _, schema := range []string{"http", "https", "mailto", "snap", "help", "apt", "zoommtg", "zoomus", "zoomphonecall", "slack", "msteams"} {78 err := s.launcher.OpenURL(schema+"://snapcraft.io", ":some-dbus-sender")79 c.Assert(err, IsNil)80 c.Assert(s.mockXdgOpen.Calls(), DeepEquals, [][]string{81 {"xdg-open", schema + "://snapcraft.io"},82 })83 s.mockXdgOpen.ForgetCalls()84 }85}86func (s *launcherSuite) testOpenURLWithFallbackHappy(c *C, desktopFileName string) {87 mockXdgMime := testutil.MockCommand(c, "xdg-mime", fmt.Sprintf(`echo "%s"`, desktopFileName))88 defer mockXdgMime.Restore()89 defer s.mockXdgOpen.ForgetCalls()90 err := s.launcher.OpenURL("fallback-scheme://snapcraft.io", ":some-dbus-sender")91 c.Assert(err, IsNil)92 c.Assert(s.mockXdgOpen.Calls(), DeepEquals, [][]string{93 {"xdg-open", "fallback-scheme://snapcraft.io"},94 })95 c.Assert(mockXdgMime.Calls(), DeepEquals, [][]string{96 {"xdg-mime", "query", "default", "x-scheme-handler/fallback-scheme"},97 })98}99func (s *launcherSuite) testOpenURLWithFallbackInvalidDesktopFile(c *C, desktopFileName string) {100 mockXdgMime := testutil.MockCommand(c, "xdg-mime", fmt.Sprintf("echo %s", desktopFileName))101 defer mockXdgMime.Restore()102 err := s.launcher.OpenURL("fallback-scheme://snapcraft.io", ":some-dbus-sender")103 c.Assert(err, ErrorMatches, `Supplied URL scheme "fallback-scheme" is not allowed`)104 c.Assert(s.mockXdgOpen.Calls(), IsNil)105 c.Assert(mockXdgMime.Calls(), DeepEquals, [][]string{106 {"xdg-mime", "query", "default", "x-scheme-handler/fallback-scheme"},107 })108}109func (s *launcherSuite) TestOpenURLWithFallback(c *C) {110 s.testOpenURLWithFallbackHappy(c, "open-this-scheme.desktop")111 s.testOpenURLWithFallbackHappy(c, "open.this.scheme.desktop")112 s.testOpenURLWithFallbackHappy(c, "org._7_zip.Archiver.desktop")113 s.testOpenURLWithFallbackInvalidDesktopFile(c, "1.2.3.4.desktop")114 s.testOpenURLWithFallbackInvalidDesktopFile(c, "1org.foo.bar.desktop")115 s.testOpenURLWithFallbackInvalidDesktopFile(c, "foo bar baz.desktop")116}117func (s *launcherSuite) TestOpenURLWithFailingXdgOpen(c *C) {118 cmd := testutil.MockCommand(c, "xdg-open", "false")119 defer cmd.Restore()120 err := s.launcher.OpenURL("https://snapcraft.io", ":some-dbus-sender")121 c.Assert(err, NotNil)122 c.Assert(err, ErrorMatches, "cannot open supplied URL")123}124func mockUICommands(c *C, script string) (restore func()) {125 mock := testutil.MockCommand(c, "zenity", script)126 mock.Also("kdialog", script)127 return func() {128 mock.Restore()129 }130}131func (s *launcherSuite) TestOpenFileUserAccepts(c *C) {132 restore := mockUICommands(c, "true")133 defer restore()134 path := filepath.Join(c.MkDir(), "test.txt")135 c.Assert(ioutil.WriteFile(path, []byte("Hello world"), 0644), IsNil)136 file, err := os.Open(path)137 c.Assert(err, IsNil)138 defer file.Close()139 dupFd, err := syscall.Dup(int(file.Fd()))140 c.Assert(err, IsNil)141 err = s.launcher.OpenFile("", dbus.UnixFD(dupFd), ":some-dbus-sender")142 c.Assert(err, IsNil)143 c.Assert(s.mockXdgOpen.Calls(), DeepEquals, [][]string{144 {"xdg-open", path},145 })146}147func (s *launcherSuite) TestOpenFileUserDeclines(c *C) {148 restore := mockUICommands(c, "false")149 defer restore()150 path := filepath.Join(c.MkDir(), "test.txt")151 c.Assert(ioutil.WriteFile(path, []byte("Hello world"), 0644), IsNil)152 file, err := os.Open(path)153 c.Assert(err, IsNil)154 defer file.Close()155 dupFd, err := syscall.Dup(int(file.Fd()))156 c.Assert(err, IsNil)157 err = s.launcher.OpenFile("", dbus.UnixFD(dupFd), ":some-dbus-sender")158 c.Assert(err, NotNil)159 c.Assert(err, ErrorMatches, "permission denied")160 c.Assert(s.mockXdgOpen.Calls(), IsNil)161}162func (s *launcherSuite) TestOpenFileSucceedsWithDirectory(c *C) {163 restore := mockUICommands(c, "true")164 defer restore()165 dir := c.MkDir()166 fd, err := syscall.Open(dir, syscall.O_RDONLY|syscall.O_DIRECTORY, 0755)167 c.Assert(err, IsNil)168 defer syscall.Close(fd)169 dupFd, err := syscall.Dup(fd)170 c.Assert(err, IsNil)171 err = s.launcher.OpenFile("", dbus.UnixFD(dupFd), ":some-dbus-sender")172 c.Assert(err, IsNil)173 c.Assert(s.mockXdgOpen.Calls(), DeepEquals, [][]string{174 {"xdg-open", dir},175 })176}177func (s *launcherSuite) TestOpenFileFailsWithDeviceFile(c *C) {178 restore := mockUICommands(c, "true")179 defer restore()180 file, err := os.Open("/dev/null")181 c.Assert(err, IsNil)182 defer file.Close()183 dupFd, err := syscall.Dup(int(file.Fd()))184 c.Assert(err, IsNil)185 err = s.launcher.OpenFile("", dbus.UnixFD(dupFd), ":some-dbus-sender")186 c.Assert(err, NotNil)187 c.Assert(err, ErrorMatches, "cannot open anything other than regular files or directories")188 c.Assert(s.mockXdgOpen.Calls(), IsNil)189}190func (s *launcherSuite) TestOpenFileFailsWithPathDescriptor(c *C) {191 restore := mockUICommands(c, "true")192 defer restore()193 dir := c.MkDir()194 fd, err := syscall.Open(dir, sys.O_PATH, 0755)195 c.Assert(err, IsNil)196 defer syscall.Close(fd)197 dupFd, err := syscall.Dup(fd)198 c.Assert(err, IsNil)199 err = s.launcher.OpenFile("", dbus.UnixFD(dupFd), ":some-dbus-sender")200 c.Assert(err, NotNil)201 c.Assert(err, ErrorMatches, "cannot use file descriptors opened using O_PATH")202 c.Assert(s.mockXdgOpen.Calls(), IsNil)203}204func (s *launcherSuite) TestFailsOnUbuntuCore(c *C) {205 restore := release.MockOnClassic(false)206 defer restore()207 path := filepath.Join(c.MkDir(), "test.txt")208 c.Assert(ioutil.WriteFile(path, []byte("Hello world"), 0644), IsNil)209 file, err := os.Open(path)210 c.Assert(err, IsNil)211 defer file.Close()212 dupFd, err := syscall.Dup(int(file.Fd()))213 c.Assert(err, IsNil)214 err = s.launcher.OpenFile("", dbus.UnixFD(dupFd), ":some-dbus-sender")215 c.Check(err, ErrorMatches, "not supported on Ubuntu Core")216 err = s.launcher.OpenURL("https://snapcraft.io", ":some-dbus-sender")217 c.Check(err, ErrorMatches, "not supported on Ubuntu Core")218 c.Check(s.mockXdgOpen.Calls(), HasLen, 0)219}...

Full Screen

Full Screen

userd_launcher_test.go

Source:userd_launcher_test.go Github

copy

Full Screen

...53 s.DBusTest.SetUpTest(c)54 s.openError = nil55 s.calls = nil56}57func (s *userdSuite) TestOpenFile(c *C) {58 launcher := &xdgopenproxy.UserdLauncher{}59 path := filepath.Join(c.MkDir(), "test.txt")60 c.Assert(ioutil.WriteFile(path, []byte("hello world"), 0644), IsNil)61 err := launcher.OpenFile(s.SessionBus, path)62 c.Check(err, IsNil)63 c.Check(s.calls, DeepEquals, []string{64 "OpenFile",65 })66}67func (s *userdSuite) TestOpenFileError(c *C) {68 s.openError = dbus.MakeFailedError(fmt.Errorf("failure"))69 launcher := &xdgopenproxy.UserdLauncher{}70 path := filepath.Join(c.MkDir(), "test.txt")71 c.Assert(ioutil.WriteFile(path, []byte("hello world"), 0644), IsNil)72 err := launcher.OpenFile(s.SessionBus, path)73 c.Check(err, FitsTypeOf, dbus.Error{})74 c.Check(err, ErrorMatches, "failure")75 c.Check(s.calls, DeepEquals, []string{76 "OpenFile",77 })78}79func (s *userdSuite) TestOpenDir(c *C) {80 launcher := &xdgopenproxy.UserdLauncher{}81 dir := c.MkDir()82 err := launcher.OpenFile(s.SessionBus, dir)83 c.Check(err, IsNil)84 c.Check(s.calls, DeepEquals, []string{85 "OpenFile",86 })87}88func (s *userdSuite) TestOpenMissingFile(c *C) {89 launcher := &xdgopenproxy.UserdLauncher{}90 path := filepath.Join(c.MkDir(), "no-such-file.txt")91 err := launcher.OpenFile(s.SessionBus, path)92 c.Check(err, ErrorMatches, "no such file or directory")93 c.Check(s.calls, HasLen, 0)94}95func (s *userdSuite) TestOpenUnreadableFile(c *C) {96 launcher := &xdgopenproxy.UserdLauncher{}97 path := filepath.Join(c.MkDir(), "test.txt")98 c.Assert(ioutil.WriteFile(path, []byte("hello world"), 0644), IsNil)99 c.Assert(os.Chmod(path, 0), IsNil)100 err := launcher.OpenFile(s.SessionBus, path)101 c.Check(err, ErrorMatches, "permission denied")102 c.Check(s.calls, HasLen, 0)103}104func (s *userdSuite) TestOpenURI(c *C) {105 launcher := &xdgopenproxy.UserdLauncher{}106 err := launcher.OpenURI(s.SessionBus, "http://example.com")107 c.Check(err, IsNil)108 c.Check(s.calls, DeepEquals, []string{109 "OpenURI http://example.com",110 })111}112func (s *userdSuite) TestOpenURIError(c *C) {113 s.openError = dbus.MakeFailedError(fmt.Errorf("failure"))114 launcher := &xdgopenproxy.UserdLauncher{}115 err := launcher.OpenURI(s.SessionBus, "http://example.com")116 c.Check(err, FitsTypeOf, dbus.Error{})117 c.Check(err, ErrorMatches, "failure")118 c.Check(s.calls, DeepEquals, []string{119 "OpenURI http://example.com",120 })121}122type fakeUserd struct {123 *userdSuite124}125func (p *fakeUserd) OpenFile(parentWindow string, clientFD dbus.UnixFD) *dbus.Error {126 p.calls = append(p.calls, "OpenFile")127 fd := int(clientFD)128 defer syscall.Close(fd)129 return p.openError130}131func (p *fakeUserd) OpenURL(uri string) *dbus.Error {132 p.calls = append(p.calls, "OpenURI "+uri)133 return p.openError134}...

Full Screen

Full Screen

xdgopenproxy_test.go

Source:xdgopenproxy_test.go Github

copy

Full Screen

...23 . "gopkg.in/check.v1"24 "github.com/snapcore/snapd/usersession/xdgopenproxy"25)26func Test(t *testing.T) { TestingT(t) }27type xdgOpenSuite struct{}28var _ = Suite(&xdgOpenSuite{})29func (s *xdgOpenSuite) TestOpenURL(c *C) {30 launcher := &fakeLauncher{}31 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "http://example.org"), IsNil)32 c.Check(launcher.calls, DeepEquals, []string{33 "OpenURI http://example.org",34 })35}36func (s *xdgOpenSuite) TestOpenFile(c *C) {37 launcher := &fakeLauncher{}38 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "/path/test.txt"), IsNil)39 c.Check(launcher.calls, DeepEquals, []string{40 "OpenFile /path/test.txt",41 })42}43func (s *xdgOpenSuite) TestOpenFileURL(c *C) {44 launcher := &fakeLauncher{}45 c.Check(xdgopenproxy.LaunchWithOne(nil, launcher, "file:///path/test.txt"), IsNil)46 c.Check(launcher.calls, DeepEquals, []string{47 "OpenFile /path/test.txt",48 })49}50func (s *xdgOpenSuite) TestStopOnFirstSuccess(c *C) {51 l1 := &fakeLauncher{err: fmt.Errorf("failure one")}52 l2 := &fakeLauncher{err: nil}53 l3 := &fakeLauncher{err: nil}54 launchers := []xdgopenproxy.DesktopLauncher{l1, l2, l3}55 err := xdgopenproxy.Launch(nil, launchers, "http://example.org")56 c.Check(err, IsNil)57 c.Check(l1.calls, DeepEquals, []string{58 "OpenURI http://example.org",59 })60 c.Check(l2.calls, DeepEquals, []string{61 "OpenURI http://example.org",62 })63 c.Check(l3.calls, HasLen, 0)64}65func (s *xdgOpenSuite) TestStopOnResponseError(c *C) {66 l1 := &fakeLauncher{err: fmt.Errorf("failure one")}67 l2 := &fakeLauncher{err: xdgopenproxy.MakeResponseError("hello")}68 l3 := &fakeLauncher{err: nil}69 launchers := []xdgopenproxy.DesktopLauncher{l1, l2, l3}70 err := xdgopenproxy.Launch(nil, launchers, "http://example.org")71 c.Check(err, Equals, l2.err)72 c.Check(l3.calls, HasLen, 0)73}74type fakeLauncher struct {75 err error76 calls []string77}78func (l *fakeLauncher) OpenFile(bus *dbus.Conn, path string) error {79 l.calls = append(l.calls, "OpenFile "+path)80 return l.err81}82func (l *fakeLauncher) OpenURI(bus *dbus.Conn, uri string) error {83 l.calls = append(l.calls, "OpenURI "+uri)84 return l.err85}...

Full Screen

Full Screen

Open

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("notepad.exe", "1.txt")4 err := cmd.Start()5 if err != nil {6 fmt.Println("Error in opening file:", err)7 }8}9import (10func main() {11 cmd := exec.Command("notepad.exe", "1.txt")12 err := cmd.Start()13 if err != nil {14 fmt.Println("Error in opening file:", err)15 }16}17import (18func main() {19 cmd := exec.Command("notepad.exe", "1.txt")20 err := cmd.Start()21 if err != nil {22 fmt.Println("Error in opening file:", err)23 }24 err = cmd.Wait()25 if err != nil {26 fmt.Println("Error in waiting for the command to complete:", err)27 }28}

Full Screen

Full Screen

Open

Using AI Code Generation

copy

Full Screen

1import (2func main() {3}4import (5func main() {6 exec.Command("rundll32", "url.dll,FileProtocolHandler", "C:\\Users\\pradeep\\Desktop\\test.txt").Start()7}8import (9func main() {10 exec.Command("rundll32", "url.dll,FileProtocolHandler", "/p", "C:\\Users\\pradeep\\Desktop\\test.txt").Start()11}12import (13func main() {14 exec.Command("rundll32", "url.dll,FileProtocolHandler", "/e", "C:\\Users\\pradeep\\Desktop\\test.txt").Start()15}16import (17func main() {18 exec.Command("rundll32", "url.dll,FileProtocolHandler", "/pt", "C:\\Users\\pradeep\\Desktop\\test.txt").Start()19}20import (21func main() {22 exec.Command("rundll32", "url.dll,FileProtocolHandler", "/e,/select,", "C:\\Users\\pradeep\\Desktop\\test.txt").Start()23}

Full Screen

Full Screen

Open

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "os/exec"3func main(){4 fmt.Println("Launching notepad")5 err := exec.Command("notepad").Start()6 if err != nil {7 fmt.Println(err)8 }9}

Full Screen

Full Screen

Open

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 launch := launcher.Launcher{Path: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"}4 launch.Open()5 fmt.Println("Exit")6}

Full Screen

Full Screen

Open

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, err := os.Create("test.txt")4 if err != nil {5 fmt.Println(err)6 }7 defer f.Close()8 l, err := f.WriteString("hello world")9 if err != nil {10 fmt.Println(err)11 f.Close()12 }13 fmt.Println(l, "bytes written successfully")14}15import (16func main() {17 f, err := os.OpenFile("test.txt", os.O_APPEND|os.O_WRONLY, 0644)18 if err != nil {19 fmt.Println(err)20 }21 defer f.Close()22 l, err := f.WriteString("hello world")23 if err != nil {24 fmt.Println(err)25 f.Close()26 }27 fmt.Println(l, "bytes written successfully")28}29import (30func main() {31 f, err := os.Create("test.txt")32 if err != nil {33 fmt.Println(err)34 }35 defer f.Close()36 l, err := f.WriteString("hello world")37 if err != nil {38 fmt.Println(err)39 f.Close()40 }41 fmt.Println(l, "bytes written successfully")42 fileInfo, err := f.Stat()43 if err != nil {44 fmt.Println(err)45 }46 fmt.Println("File name: ", fileInfo.Name())47 fmt.Println("Size in bytes: ", fileInfo.Size())48 fmt.Println("Permissions: ", fileInfo.Mode())49 fmt.Println("Last modified: ", fileInfo.ModTime())50 fmt.Println("Is Directory: ", fileInfo.IsDir())51 fmt.Println("System

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful