Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 440 lines (354 sloc) 14.871 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require 'set'
require 'fileutils'

module Sonar
  module Connector
    describe "SonarConnectorFilestore" do

      before(:all) do
        FileStore::logger.level = Logger::FATAL
      end

      before(:each) do
        FileUtils.rm_rf(TMP_DIR)
        FileUtils.mkdir_p(TMP_DIR)
      end

      after(:each) do
      end

      def create_testfs(*areas)
        areas = [:foo, :bar] if areas.empty?
        FileStore.new(TMP_DIR, :testfs, areas)
      end

      describe "valid_area_name?" do
        it "should not permit :tmp as an area name" do
          FileStore.valid_area_name?(:tmp).should == false
        end

        it "should permit :foo as an area name" do
          FileStore.valid_area_name?(:foo).should == true
        end
      end

      describe "ordinary_directory_name?" do
        it "should return false for .." do
          FileStore.ordinary_directory_name?("..").should == false
        end

        it "should return false for ." do
          FileStore.ordinary_directory_name?(".").should == false
        end

        it "should return false for .foo" do
          FileStore.ordinary_directory_name?(".foo").should == false
       end

        it "should return true for foo" do
          FileStore.ordinary_directory_name?("foo").should == true
        end

        it "should return false for foo/." do
          FileStore.ordinary_directory_name?("foo/.").should == false
        end

        it "should return false for foo/.foo" do
          FileStore.ordinary_directory_name?("foo/.foo").should == false
        end
      end

      describe "valid_filestore_name?" do
        it "should not permitted sub-directory names" do
          FileStore.valid_filestore_name?("foo/bar").should == false
        end

        it "should permit simple filenames" do
          FileStore.valid_filestore_name?("foo").should == true
        end

        it "should not permit dot filenames" do
          FileStore.valid_filestore_name?(".foo").should == false
        end

        it "should not permit special dirnames" do
          FileStore.valid_filestore_name?(".").should == false
        end
      end

      describe "ordinary_directory?" do
        it "should return true for regular real directory" do
          FileStore.ordinary_directory?(TMP_DIR).should == true
        end

        it "should return false for nested special directory" do
          FileStore.ordinary_directory?(File.join(TMP_DIR, '.')).should == false
        end

        it "should return false for non-existent directory" do
          FileStore.ordinary_directory?("blahblah").should == false
        end
      end


      it "should initialize with a root, a name and areas, and create directories" do
        fs=create_testfs
        fs.root.should == TMP_DIR
        fs.name.should == :testfs
        fs.areas.should == [:foo, :bar].to_set
        File.directory?(File.join(TMP_DIR, "testfs")).should == true
        File.directory?(File.join(TMP_DIR, "testfs", "foo")).should == true
        File.directory?(File.join(TMP_DIR, "testfs", "bar")).should == true
      end

      it "should destroy itself cleanly" do
        fs=create_testfs
        fs.destroy!
        File.exist?(File.join(TMP_DIR, "testfs")).should == false
      end

      it "should write files to an area" do
        fs = create_testfs
        fs.write(:foo, "testfile.txt", "one two three")
        File.read(File.join(TMP_DIR, "testfs", "foo", "testfile.txt")).should == "one two three"
      end
      
      it "should count files in an area" do
        fs = create_testfs
        fs.write(:foo, "testfile.txt", "one two three")
        fs.write(:foo, "testfile2.txt", "one two three")
        fs.count(:foo).should == 2
      end

      it "should count files across all areas" do
        fs = create_testfs
        fs.write(:foo, "testfile.txt", "one two three")
        fs.write(:bar, "testfile2.txt", "one two three")
        fs.area_count.should == {:foo=>1, :bar=>1}
      end
      
      it "should give a kb based disk usage for an area" do
        # don't actually know what disk page size is
        # so test that each file occupies >0 space, and that two
        # files the same size occupy twice as much as one
        fs = create_testfs
        fs.write(:foo, "testfile.txt", "one two three")
        sz1 = fs.size(:foo)
        sz1.should > 0
        fs.write(:foo, "testfile2.txt", "one two three")
        fs.size(:foo).should == 2*sz1
      end

      it "should give kb based disk usage across all areas" do
        fs = create_testfs
        fs.write(:foo, "testfile.txt", "one two three")
        sz1 = fs.size(:foo)
        sz1.should > 0
        fs.write(:bar, "testfile.txt", "one two three")
        fs.write(:bar, "testfile2.txt", "one two three")
        fs.area_size.should == {:foo=>sz1, :bar=>2*sz1}
      end

      it "should scrub empty directories from an area" do
        fs = create_testfs
        ap = fs.area_path(:foo)
        FileUtils.mkdir_p(File.join(ap, "bar", "baz"))
        FileUtils.mkdir_p(File.join(ap, "woo"))
        FileUtils.mkdir_p(File.join(ap, "waz"))
        fs.write(:foo, File.join("waz", "testfile.txt"), "one two three")

        fs.scrub!(:foo)

        File.exist?(File.join(ap, "bar")).should == false
        File.exist?(File.join(ap, "woo")).should == false
        File.exist?(File.join(ap, "waz", "testfile.txt")).should == true
      end


      it "should iterate over all files in an area" do
        fs = create_testfs
        fs.write(:foo, "testfile.txt", "one two three")
        fs.write(:foo, "testfile2.txt", "four five six")

        texts = Set.new
        ap = fs.area_path(:foo)
        fs.for_each(:foo) do |f|
          texts << File.read(File.join(ap, f))
        end

        texts.should == ["one two three", "four five six"].to_set
      end

      it "should ignore . and .. files when iterating" do
        fs = create_testfs(:foo, :bar, :baz)
        ap = fs.area_path(:foo)

        # only files "foo" and "bar" exist in area :foo
        stub(File).file?(File.join(ap,"foo")){true}
        stub(File).file?(File.join(ap,"bar")){true}
        stub(File).file?(){false}

        stub(Dir).foreach do |path, proc|
          [".", "..", "foo", "bar"].each{ |p| proc.call(p)}
        end
        
        files = Set.new
        fs.for_each(:foo){|f| files << f}
        files.should == ["foo", "bar"].to_set
        
      end

      describe "process" do
        before do
          @fs = create_testfs(:foo, :bar, :baz)
          @fs.write(:foo, "testfile.txt", "one two three")
          @fs.write(:foo, "testfile2.txt", "four five six")
          @fs.write(:foo, "testfile3.txt", "seven eight nine")
        end

        it "should process all files in an area" do
          texts = Set.new
          @fs.process(:foo) do |f|
            texts << File.read(@fs.file_path(:foo, f))
          end
          texts.should == ["one two three", "four five six", "seven eight nine"].to_set
          @fs.count(:foo).should == 0
        end

        it "should move failed processings to the error_area" do
          texts = Set.new
          lambda {
            @fs.process(:foo, :bar) do |f|
              s = File.read(@fs.file_path(:foo, f))
              raise "five" if s =~ /five/
              texts << s
            end
          }.should raise_error("five")
          @fs.count(:foo).should == 1
          @fs.count(:bar).should == 1
          @fs.read(:bar, "testfile2.txt").should == "four five six"
        end

        it "should move completed processings to the success_area" do
          texts = Set.new
          lambda {
            @fs.process(:foo, :bar, :baz) do |f|
              s = File.read(@fs.file_path(:foo, f))
              raise "five" if s =~ /five/
              texts << s
            end
          }.should raise_error("five")
          @fs.count(:foo).should == 1
          @fs.count(:bar).should == 1
          @fs.count(:baz).should == 1
          @fs.read(:bar, "testfile2.txt").should == "four five six"
        end
      end

      describe "process_batch" do
        before do
          @fs = create_testfs(:foo, :bar, :baz)
          FileUtils.mkdir_p(File.join(@fs.area_path(:foo), "a", "b"))
          FileUtils.mkdir_p(File.join(@fs.area_path(:foo), "c", "d"))
          @fs.write(:foo, "a/b/testfile.txt", "one two three")
          @fs.write(:foo, "a/testfile2.txt", "four five six")
          @fs.write(:foo, "c/d/testfile3.txt", "seven eight nine")
          @fs.write(:foo, "c/testfile3.txt", "ten eleven twelve")

          @files = ["a/b/testfile.txt", "a/testfile2.txt", "c/d/testfile3.txt", "c/testfile3.txt"].to_set
          @processed = Set.new
        end

        def process_batch(fs, size, source_area, error_area=nil, success_area=nil)
          fs.process_batch(size, source_area, error_area, success_area) do |batch|
            @processed += batch
            batch.each do |f|
              raise "#{f} not in @files" if !@files.delete?(f)
            end
            if block_given?
              batch.each do |f|
                yield f
              end
            end
          end
        end

        def check_files(fs, area, paths, test)
          paths.each{|f| File.exist?(fs.file_path(area, f)).should == test}
        end

        it "should process and delete a limited batch of files" do
          process_batch(@fs, 2, :foo).should == 2
          
          @processed.size.should == 2
          @files.size.should == 2
          check_files(@fs, :foo, @processed, false)
          check_files(@fs, :foo, @files, true)

          process_batch(@fs, 3, :foo).should == 2

          @processed.size.should == 4
          @files.size.should == 0
          check_files(@fs, :foo, @processed, false)

          process_batch(@fs, 2, :foo).should == 0
        end

        it "should not call the block if there are no files to process" do
          process_batch(@fs, 4, :foo).should == 4
          @fs.process_batch(1, :foo) do
            raise "i should not be called"
          end
        end

        it "should move failed batches to error_area if given" do
          lambda {
            process_batch(@fs, 2, :foo, :bar){|f| raise "foo"}
          }.should raise_error("foo")
          
          @files.size.should == 2
          check_files(@fs, :foo, @files, true)
          check_files(@fs, :bar, @processed, true)

          @ok = Set.new
          process_batch(@fs, 2, :foo, :bar){|f| @ok << f}.should == 2
          @files.size.should == 0
          check_files(@fs, :foo, @ok, false)
          check_files(@fs, :bar, @ok, false)

          process_batch(@fs, 2, :foo, :bar).should == 0
        end

        it "should leave failed batches alone if error_area is the same as source_area" do
          @files_copy = @files.clone
          lambda {
            process_batch(@fs, 2, :foo, :foo){|f| raise "foo"}
          }.should raise_error("foo")
          
          @fs.area_files(:foo).length.should == 4
          check_files(@fs, :foo, @files_copy, true)

          @ok = Set.new
          @files = @files_copy.clone
          process_batch(@fs, 4, :foo, :foo){|f| @ok << f}.should == 4
          @files.size.should == 0
          @ok.size.should == 4
          check_files(@fs, :foo, @ok, false)

          process_batch(@fs, 2, :foo, :foo).should == 0
        end

        it "should move successful batches to success_area if given" do
          process_batch(@fs, 2, :foo, :bar, :baz).should == 2
          @files.size.should == 2
          check_files(@fs, :foo, @files, true)
          check_files(@fs, :baz, @processed, true)

          process_batch(@fs, 2, :foo, :bar, :baz).should == 2
          check_files(@fs, :baz, @processed, true)
          
          process_batch(@fs, 2, :foo, :bar, :baz).should == 0
        end

        it "should leave files in source area if LeaveInSourceArea is thrown" do
          lambda {
            process_batch(@fs, 2, :foo, :bar){|f| raise FileStore::LeaveInSourceArea }
          }.should raise_error(FileStore::LeaveInSourceArea)
          check_files(@fs, :foo, @files, true)
          check_files(@fs, :baz, @processed, false)
          check_files(@fs, :foo, @processed, true)
        end

      end

      describe "flip" do
        before do
          @testfs = create_testfs(:foo, :bar, :baz)
          @testfs.write(:foo, "testfile.txt", "one two three")
          
          @targetfs = FileStore.new(TMP_DIR, :targetfs, [:a, :b])
        end


        it "should flip non-unique names from testfs to uniquely named subdir of targetfs" do
          stub(@targetfs).unique_name{"some-uuid-string"}

          @testfs.flip(:foo, @targetfs, :a, false)

          File.exists?(File.join(@targetfs.area_path(:a), "some-uuid-string", "testfile.txt")).should == true

          # should recreate area in flipped source, so source is
          # still valid
          File.exists?(File.join(@testfs.area_path(:foo)))
        end

        it "should flip unique names from testfs to targetfs without introducing additional subdirectories" do
          @testfs.flip(:foo, @targetfs, :a)

          File.exists?(File.join(@targetfs.area_path(:a), "testfile.txt")).should == true

          # should recreate area in flipped source, so source is
          # still valid
          File.exists?(File.join(@testfs.area_path(:foo)))
        end

      end

      describe "area_files" do
        before do
          @fs = create_testfs(:foo)
          FileUtils.mkdir_p(File.join(@fs.area_path(:foo), "a", "b"))
          FileUtils.mkdir_p(File.join(@fs.area_path(:foo), "c", "d"))
          @fs.write(:foo, "a/b/testfile.txt", "one two three")
          @fs.write(:foo, "a/testfile2.txt", "four five six")
          @fs.write(:foo, "c/d/testfile3.txt", "seven eight nine")
          @fs.write(:foo, "c/testfile3.txt", "ten eleven twelve")

          @files = ["a/b/testfile.txt", "a/testfile2.txt", "c/d/testfile3.txt", "c/testfile3.txt"].to_set
        end

        it "should fetch all paths if max not given" do
          @fs.area_files(:foo).to_set.should == @files
        end

        it "should fetch a limited number of paths if max given" do
          fs = @fs.area_files(:foo, 2).to_set
          fs.size.should == 2
          (@files - fs).size.should == 2
        end
      end

      describe "logger" do
        it "should write to the class logger if no instance logger given" do
          fs = create_testfs(:foo)
          mock(FileStore.logger).debug("foo")
          fs.logger.debug("foo")
        end

        it "should write to the instance logger if given" do
          fs = create_testfs(:foo)
          mock(logger = Object.new).debug("foo")
          fs.logger = logger
          fs.logger.debug("foo")
        end
      end

    end
  end
end
Something went wrong with that request. Please try again.