diff options
author | Malcolm Reynolds <malcolm.reynolds@gmail.com> | 2016-03-29 19:28:41 +0300 |
---|---|---|
committer | Malcolm Reynolds <malcolm.reynolds@gmail.com> | 2016-03-29 19:36:05 +0300 |
commit | d451113f625cf006a0eff232d4cc15eb1078dc11 (patch) | |
tree | edbbf5f1ab53ac2ac8643ac4e15f5b07040db2b6 | |
parent | d356f63829b2d1356058500a0af9f06b4cd504b9 (diff) |
Added fix for silent failure when output file is in nonexistent dir.
Previously if you say 'gnuplot.pngfigure("nonexistent_dir/foo.png")'
this would silently fail - an error message is generated in gnuplot but
we cannot read anything back via the readonly PipeFile. We now treat
this as an error if trying to write to a nonexistent directory.
-rw-r--r-- | gnuplot.lua | 12 | ||||
-rw-r--r-- | test.lua | 52 |
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() |