<commit_before>require 'spec_helper'

describe 'Path : Dir', :tmpchdir do
  it 'glob' do
    f = Path('f')
    f.write 'abc'
    d = Path('d').mkdir

    Path.glob('*').sort.should == [d,f]
    Path.glob('*', &accumulator)
    accumulator.sort.should == [d,f]

    a = Path('a.rb').touch
    b = Path('b.rb').touch

    Path.glob('*.rb').sort.should == [a,b]
    Path.getwd.glob('*.rb').sort.should == [a,b].map(&:expand)
    Path.getwd.glob('*.rb', &accumulator)
    accumulator.sort.should == [a,b].map(&:expand)
  end

  it 'getwd, cwd, pwd', :tmpchdir => false do
    Path.method(:getwd).should == Path.method(:cwd)
    Path.method(:getwd).should == Path.method(:pwd)
    Path.getwd.should be_kind_of Path
  end

  it 'entries' do
    a, b = Path('a').touch, Path('b').touch
    Path('.').entries.sort.should == [Path('.'), Path('..'), a, b]
  end

  it 'each_entry' do
    a, b = Path('a').touch, Path('b').touch
    Path('.').each_entry(&accumulator)
    accumulator.sort.should == [Path('.'), Path('..'), a, b]
  end

  it 'mkdir' do
    Path('d').mkdir.should be_a_directory
    Path('e').mkdir(0770).should be_a_directory
  end

  it 'rmdir' do
    d = Path('d').mkdir
    d.should be_a_directory
    d.rmdir
    d.should_not exist
  end

  it 'opendir' do
    Path('a').touch
    Path('b').touch
    Path('.').opendir { |d|
      d.each(&accumulator)
    }
    accumulator.sort.should == ['.', '..', 'a', 'b']
  end

  it 'chdir', :tmpchdir => false do
    called = false
    spec = Path(__FILE__).expand.dir.parent
    root = spec.parent
    Path.getwd.should == root
    spec.chdir do
      called = true
      Path.getwd.should == spec
    end
    Path.getwd.should == root
    called.should be_true
  end

  it 'children' do
    a = Path('a').touch
    b = Path('b').touch
    d = Path('d').mkdir
    x = Path('d/x').touch
    y = Path('d/y').touch

    Path('.').children.sort.should == [a, b, d]
    d.children.sort.should == [x, y]
    d.children(false).sort.should == [Path('x'), Path('y')]
  end

  it 'each_child' do
    a = Path('a').touch
    b = Path('b').touch
    d = Path('d').mkdir
    x = Path('d/x').touch
    y = Path('d/y').touch

    Path('.').each_child(&accumulator)
    accumulator.sort.should == [a, b, d]

    d.each_child(&accumulator)
    accumulator.sort.should == [x, y]
<commit_msg>clean dir specs by using #let and helpers<commit_after>require 'spec_helper'

describe 'Path : Dir', :tmpchdir do
  let(:a) { Path('a').touch }
  let(:b) { Path('b').touch }
  let(:d) { Path('d').mkdir }
  let(:x) { (d/'x').touch }
  let(:y) { (d/'y').touch }

  def create_files
    a; b
  end

  def create_hierarchy
    a; b; d; x; y
  end

  it 'glob' do
    f = Path('f')
    f.write 'abc'
    d = Path('d').mkdir

    Path.glob('*').sort.should == [d,f]
    Path.glob('*', &accumulator)
    accumulator.sort.should == [d,f]

    a = Path('a.rb').touch
    b = Path('b.rb').touch

    Path.glob('*.rb').sort.should == [a,b]
    Path.getwd.glob('*.rb').sort.should == [a,b].map(&:expand)
    Path.getwd.glob('*.rb', &accumulator)
    accumulator.sort.should == [a,b].map(&:expand)
  end

  it 'getwd, cwd, pwd', :tmpchdir => false do
    Path.method(:getwd).should == Path.method(:cwd)
    Path.method(:getwd).should == Path.method(:pwd)
    Path.getwd.should be_kind_of Path
  end

  it 'entries' do
    create_files
    Path('.').entries.sort.should == [Path('.'), Path('..'), a, b]
  end

  it 'each_entry' do
    create_files
    Path('.').each_entry(&accumulator)
    accumulator.sort.should == [Path('.'), Path('..'), a, b]
  end

  it 'mkdir' do
    d.should be_a_directory
    Path('e').mkdir(0770).should be_a_directory
  end

  it 'rmdir' do
    d.should be_a_directory
    d.rmdir
    d.should_not exist
  end

  it 'opendir' do
    create_files
    Path('.').opendir { |dir|
      dir.each(&accumulator)
    }
    accumulator.sort.should == ['.', '..', 'a', 'b']
  end

  it 'chdir', :tmpchdir => false do
    called = false
    spec = Path(__FILE__).expand.dir.parent
    root = spec.parent
    Path.getwd.should == root
    spec.chdir do
      called = true
      Path.getwd.should == spec
    end
    Path.getwd.should == root
    called.should be_true
  end

  it 'children' do
    create_hierarchy
    Path('.').children.sort.should == [a, b, d]
    d.children.sort.should == [x, y]
    d.children(false).sort.should == [Path('x'), Path('y')]
  end

  it 'each_child' do
    create_hierarchy
    Path('.').each_child(&accumulator)
    accumulator.sort.should == [a, b, d]

    d.each_child(&accumulator)
    accumulator.sort.should == [x, y]

{'fraction_non_alphanumeric': 0.19773272291257904, 'fraction_numerical': 0.0017440592980161324, 'mean_word_length': 2.685140562248996, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3586489', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1734, 'n_words': 361}