From ff7797eac5001c1471722904968aa1621d3f7bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Roth?= Date: Wed, 4 Dec 2024 14:23:36 +0100 Subject: [PATCH] graph: do not remove tempfile when opening in viewer --- cmd/graph.go | 18 +++--------------- system/t14_graph/CreateGraphTest_gold | 2 ++ system/t14_graph/graph.py | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 15 deletions(-) create mode 100644 system/t14_graph/CreateGraphTest_gold create mode 100644 system/t14_graph/graph.py diff --git a/cmd/graph.go b/cmd/graph.go index 4425d087b8..64b12d9ed8 100644 --- a/cmd/graph.go +++ b/cmd/graph.go @@ -9,7 +9,6 @@ import ( "path/filepath" "runtime" "strings" - "time" "github.com/aptly-dev/aptly/deb" "github.com/aptly-dev/aptly/utils" @@ -79,10 +78,6 @@ func aptlyGraph(cmd *commander.Command, args []string) error { return err } - defer func() { - _ = os.Remove(tempfilename) - }() - if output != "" { err = utils.CopyFile(tempfilename, output) if err != nil { @@ -90,23 +85,16 @@ func aptlyGraph(cmd *commander.Command, args []string) error { } fmt.Printf("Output saved to %s\n", output) + _ = os.Remove(tempfilename) } else { command := getOpenCommand() - fmt.Printf("Rendered to %s file: %s, trying to open it with: %s %s...\n", format, tempfilename, command, tempfilename) + fmt.Printf("Displaying %s file: %s %s\n", format, command, tempfilename) args := strings.Split(command, " ") viewer := exec.Command(args[0], append(args[1:], tempfilename)...) viewer.Stderr = os.Stderr - if err = viewer.Start(); err == nil { - // Wait for a second so that the visualizer has a chance to - // open the input file. This needs to be done even if we're - // waiting for the visualizer as it can be just a wrapper that - // spawns a browser tab and returns right away. - defer func(t <-chan time.Time) { - <-t - }(time.After(time.Second)) - } + err = viewer.Start() } return err diff --git a/system/t14_graph/CreateGraphTest_gold b/system/t14_graph/CreateGraphTest_gold new file mode 100644 index 0000000000..12928d29ec --- /dev/null +++ b/system/t14_graph/CreateGraphTest_gold @@ -0,0 +1,2 @@ +Generating graph... +Displaying png file: xdg-open /tmp/aptly-graph1173098610.png diff --git a/system/t14_graph/graph.py b/system/t14_graph/graph.py new file mode 100644 index 0000000000..e353582898 --- /dev/null +++ b/system/t14_graph/graph.py @@ -0,0 +1,19 @@ +""" +Test aptly graph +""" + +import os + +from lib import BaseTest + + +class CreateGraphTest(BaseTest): + """ + open graph in viewer + """ + fixtureCmds = ["mkdir -p ../build", "ln -s /bin/true ../build/xdg-open"] + environmentOverride = {"PATH": os.environ["PATH"] + ":../build"} + runCmd = "aptly graph" + + def teardown(self): + self.run_cmd(["rm", "-f", "../build/xdg-open"])