module Cuke
  module Run
    class Base
      RERUN_FILE = File.join(Support::Paths.logs_root, "rerun.txt")

      def initialize(opts_hash)
        unless opts_hash[:cucumber_opts] && opts_hash[:cucumber_opts].is_a?(Array)
          fail "Invalid cucumber_opts supplied!: #{opts_hash[:cucumber_opts].inspect}"
        end

        @log_directory = log_directory
        @cucumber_opts = opts_hash[:cucumber_opts] + formatter
        @cucumber_opts << "PRESERVE_WORKSPACE=true" # otherwise threads compete with each other
        @files = opts_hash[:files]
      end

      def log(filename)
        File.join(@log_directory, filename)
      end

      private

      # need to separate folders for tasks that have multiple unique runs. see ci:wsr for example.
      def log_directory
        stamp = Time.now.strftime("%s%6N")
        File.join(Support::Paths.logs_root, stamp)
      end

      def create_log_directory
        FileUtils.mkdir_p @log_directory
      end
    end
  end
end
