# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/util-path"
require "qwik/page-files"

module Qwik
  class SiteAttach
    include Enumerable
    include AttachModule

    def initialize(site_dir)
      @site_dir = site_dir
      @attach_path = @site_dir.path+".attach"
    end
  end
end

if $0 == __FILE__
  require "test/unit"
  $LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
  require "qwik/test-module-path"
  $debug = true
end

if $debug
  class TC_SiteAttach < Test::Unit::TestCase
    def setup
      @dir = "./test/".path
      @dir.setup
      @attach = Qwik::SiteAttach.new(@dir.to_s)
    end

    def teardown
      @dir.teardown
    end

    def assert_exist(e, s)
      assert_equal(e, @attach.exist?(s))
    end

    def test_site_attach
      d = @dir+".attach"
      d.teardown if d.exist?

      assert_exist(false, "t.txt")

      # test put
      @attach.put("t.txt", "t")
      assert_exist(true, "t.txt")

      # test get
      path = @attach.path("t.txt")
      assert_equal("./test/.attach/t.txt", path.to_s)
      assert_equal("t", path.open {|f| f.read })

      # test delete
      @attach.delete("t.txt")
      assert_exist(false, "t.txt")

      # test_security
      path = @attach.path("t/t.txt") # ok
      assert_raise(Qwik::CanNotAccessParentDirectory) {
	path = @attach.path("../t.txt") # bad
      }
    end
  end
end
