<commit_before>require 'spec_helper'

describe Bash::Session do
  before(:each) do
    @session = Bash::Session.new
  end

  it 'should run commands and show out put when it does not end in a new line' do
    @session.execute("echo -n hello") { |output| expect(output).to eq("hello") }
  end

  it 'should return the exit status of the command' do
    exit_status = @session.execute("true")
    expect(exit_status).to eq(0)

    exit_status = @session.execute("false")
    expect(exit_status).to eq(1)
  end

  it 'should run commands ending with comments' do
    exit_status = @session.execute("true # this is a comment") { |output| expect(output).to eq("") }
    expect(exit_status).to eq(0)
  end

  it 'should preserve the state of bash internal commands' do
    @session.execute("cd #{Dir.pwd}/spec")
    @session.execute("pwd") { |output| expect(output).to include("#{Dir.pwd}/spec") }
    @session.execute("cd ~")
    @session.execute("pwd") { |output| expect(output).to include(Dir.home) }
    @session.execute("echo $FOO") { |output| expect(output).to_not include("bar") }
    @session.execute("export FOO=bar")
    @session.execute("echo $FOO") { |output| expect(output).to include("bar") }
  end

  it 'should wait for long running commands to complete and then exit' do
    reader, writer = IO.pipe
    exit_status = @session.execute("for i in {1..5}; do echo -n 'hello world '; sleep 1; done", out: writer)
    expect(exit_status).to eq(0)
    writer.close<commit_msg>Add a spec to check that it runs multiple commands separated by a newline<commit_after>require 'spec_helper'

describe Bash::Session do
  before(:each) do
    @session = Bash::Session.new
  end

  it 'should run commands and show out put when it does not end in a new line' do
    @session.execute("echo -n hello") { |output| expect(output).to eq("hello") }
  end

  it 'should return the exit status of the command' do
    exit_status = @session.execute("true")
    expect(exit_status).to eq(0)

    exit_status = @session.execute("false")
    expect(exit_status).to eq(1)
  end

  it 'should run commands ending with comments' do
    exit_status = @session.execute("true # this is a comment") { |output| expect(output).to eq("") }
    expect(exit_status).to eq(0)
  end

  it 'should preserve the state of bash internal commands' do
    @session.execute("cd #{Dir.pwd}/spec")
    @session.execute("pwd") { |output| expect(output).to include("#{Dir.pwd}/spec") }
    @session.execute("cd ~")
    @session.execute("pwd") { |output| expect(output).to include(Dir.home) }
    @session.execute("echo $FOO") { |output| expect(output).to_not include("bar") }
    @session.execute("export FOO=bar")
    @session.execute("echo $FOO") { |output| expect(output).to include("bar") }
  end

  it 'should run multiple commands separated by a newline' do
    reader, writer = IO.pipe
    @session.execute("echo hi\necho bye", out: writer)
    writer.close
    expect(reader.read).to eq("hi\nbye\n")
  end

  it 'should wait for long running commands to complete and then exit' do
    reader, writer = IO.pipe
    exit_status = @session.execute("for i in {1..5}; do echo -n 'hello world '; sleep 1; done", out: writer)
    expect(exit_status).to eq(0)
    writer.close
{'fraction_non_alphanumeric': 0.14250386398763523, 'fraction_numerical': 0.004327666151468315, 'mean_word_length': 3.844311377245509, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5355275', 'n_tokens_mistral': 1043, 'n_tokens_neox': 1016, 'n_words': 318}