Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/torch/gnuplot.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSoumith Chintala <soumith@gmail.com>2016-03-29 20:05:18 +0300
committerSoumith Chintala <soumith@gmail.com>2016-03-29 20:05:18 +0300
commit1955fb2fdf6dcdc5241727d09a838987700cb715 (patch)
treeedbbf5f1ab53ac2ac8643ac4e15f5b07040db2b6
parentd356f63829b2d1356058500a0af9f06b4cd504b9 (diff)
parentd451113f625cf006a0eff232d4cc15eb1078dc11 (diff)
Merge pull request #25 from malcolmreynolds/fix-silent-failure
Added fix for silent failure when output file is in nonexistent dir.
-rw-r--r--gnuplot.lua12
-rw-r--r--test.lua52
2 files changed, 64 insertions, 0 deletions
diff --git a/gnuplot.lua b/gnuplot.lua
index bc1f861..578f56c 100644
--- a/gnuplot.lua
+++ b/gnuplot.lua
@@ -732,6 +732,18 @@ local function filefigure(fname,term,n)
if not _gptable.hasrefresh then
print('Plotting to files is disabled in gnuplot 4.2, install gnuplot 4.4')
end
+
+ -- Check whether the output directory can be written to here - torch.PipeFile
+ -- has no read/write option so we can't read back any error messages from
+ -- gnuplot, and writing to 'non_existent_dir/plot.png' fails silently.
+ local outputDir = paths.dirname(fname)
+ if outputDir == '' then
+ outputDir = '.'
+ end
+ if not paths.dirp(outputDir) then
+ error('cannot save to ' .. fname .. ': directory does not exist')
+ end
+
local gp = getfigure(n)
gp.fname = fname
gp.term = term
diff --git a/test.lua b/test.lua
new file mode 100644
index 0000000..2d83ec3
--- /dev/null
+++ b/test.lua
@@ -0,0 +1,52 @@
+require 'gnuplot'
+require 'os'
+require 'paths'
+require 'torch'
+
+local tester = torch.Tester()
+local tests = {}
+
+-- Returns a random string of lowercase digits
+local function randomFilenameStr()
+ local t = {}
+ for i = 1, 10 do
+ table.insert(t, string.char(math.random(97, 122)))
+ end
+ return table.concat(t)
+end
+
+-- Make sure we can write to a new filename, but not to a nonexistent directory.
+function tests.cannotWriteToNonExistentDir()
+ -- Save locally, this should work
+ local validFilename = randomFilenameStr() .. '.png'
+
+ -- If this already exists (bad luck!), don't let the test overwrite it
+ assert(not (paths.filep(validFilename) or
+ paths.dirp(validFilename)),
+ 'random filename aready exists (?)')
+
+ -- Should work fine
+ gnuplot.pngfigure(validFilename)
+ gnuplot.plot({'Sin Curve',torch.sin(torch.linspace(-5,5))})
+ gnuplot.plotflush()
+
+ -- Clean up after ourselves
+ os.remove(validFilename)
+
+ -- Now make an invalid output
+ local nonExistentDir = randomFilenameStr()
+ assert(not (paths.filep(nonExistentDir) or
+ paths.dirp(nonExistentDir)),
+ 'random dir aready exists (?)')
+
+ -- This makes an absolute path below cwd, seems Lua has no way (?) to query
+ -- the file separator charater by itself...
+ local invalidFilename = paths.concat(nonExistentDir, validFilename)
+ local function shouldCrash()
+ gnuplot.pngfigure(invalidFilename)
+ end
+ tester:assertErrorPattern(shouldCrash, 'directory does not exist')
+end
+
+tester:add(tests)
+return tester:run()