Commit 183b051

mo <mo.khan@gmail.com>
2018-02-10 18:41:21
add directory option to move test files to tmpdir.
1 parent d5dc55d
lib/tfa/cli.rb
@@ -4,6 +4,7 @@ module TFA
   class CLI < Thor
     package_name "TFA"
     class_option :filename
+    class_option :directory
 
     desc "add NAME SECRET", "add a new secret to the database"
     def add(name, secret)
@@ -35,7 +36,10 @@ module TFA
     private
 
     def storage
-      @storage ||= Storage.new(filename: options[:filename] || 'tfa')
+      @storage ||= Storage.new(
+        filename: options[:filename] || 'tfa',
+        directory: options[:directory] || Dir.home,
+      )
     end
 
     def clean(secret)
lib/tfa/storage.rb
@@ -2,12 +2,12 @@ module TFA
   class Storage
     include Enumerable
 
-    def initialize(options)
-      pstore_path = File.join(Dir.home, ".#{options[:filename]}.pstore")
+    def initialize(filename: 'tfa', directory: Dir.home)
+      pstore_path = File.join(directory, ".#{filename}.pstore")
       if File.exist?(pstore_path)
         @storage = PStore.new(pstore_path)
       else
-        path = File.join(Dir.home, ".#{options[:filename]}.yml")
+        path = File.join(directory, ".#{filename}.yml")
         @storage = YAML::Store.new(path)
       end
     end
spec/lib/cli_spec.rb
@@ -1,6 +1,6 @@
 module TFA
   describe CLI do
-    subject { CLI.new([], filename: SecureRandom.uuid) }
+    subject { CLI.new([], filename: SecureRandom.uuid, directory: Dir.tmpdir) }
 
     def code_for(secret)
       ::ROTP::TOTP.new(secret).now
spec/lib/totp_command_spec.rb
@@ -1,7 +1,7 @@
 module TFA
   describe TotpCommand do
     subject { TotpCommand.new(storage) }
-    let(:storage) { Storage.new(filename: SecureRandom.uuid) }
+    let(:storage) { Storage.new(filename: SecureRandom.uuid, directory: Dir.tmpdir) }
 
     def code_for(secret)
       ::ROTP::TOTP.new(secret).now
spec/spec_helper.rb
@@ -17,6 +17,7 @@
 require 'tfa'
 require 'securerandom'
 require 'tempfile'
+require 'tmpdir'
 RSpec.configure do |config|
 # The settings below are suggested to provide a good initial experience
 # with RSpec, but feel free to customize to your heart's content.