<commit_before>Code.require_file "../test_helper.exs", __DIR__

import PathHelpers

defmodule Kernel.CLI.InitTest do
  use ExUnit.Case, async: true

  test :code_init do
    assert elixir('-e "IO.puts [?3]"') == '3\n'

    result = elixir('-e "IO.puts inspect(System.argv)" #{fixture_path("init_sample.exs")} -o 1 2 3')
    assert result == '#{inspect ["-o", "1", "2", "3"]}\n3\n'
  end
end

defmodule Kernel.CLI.OptionParsingTest do
  use ExUnit.Case, async: true

  test :path do
    root = fixture_path("../../..") |> to_char_list
    list = elixir('-pa "#{root}/*" -pz "#{root}/lib/*" -e "IO.inspect(:code.get_path, limit: :infinity)"')
    { path, _ } = Code.eval_string list, []

    # pa
    assert Path.expand('ebin', root) in path
    assert Path.expand('lib', root) in path
    assert Path.expand('src', root) in path

    # pz
    assert Path.expand('lib/list', root) in path
  end
end

defmodule Kernel.CLI.AtExitTest do
  use ExUnit.Case, async: true

  test :at_exit do
    assert elixir(fixture_path("at_exit.exs") |> to_char_list) ==
      'goodbye cruel world with status 0\n'
  end
end

defmodule Kernel.CLI.ErrorTest do
  use ExUnit.Case, async: true

  test :code_error do
    assert :string.str('** (throw) 1', elixir('-e "throw 1"')) == 0
    assert :string.str('** (ErlangError) erlang error: 1', elixir('-e "error 1"')) == 0

    # It does not catch exits with integers nor strings...
    assert elixir('-e "exit 1"') == ''
  end
end

defmodule Kernel.CLI.SyntaxErrorTest do
  use ExUnit.Case, async: true

  defp check_output(elixir_cmd, expected_msg) do
    o = elixir(elixir_cmd)
    assert :string.str(o, expected_msg) == 1, "Expected this output: `#{expected_msg}`\nbut got this output: `#{o}`"
  end

  test :syntax_code_error do
    check_output('-e "[1,2"', '** (TokenMissingError) nofile:1: missing terminator: ]')
    check_output('-e "case 1 end"', %C"** (SyntaxError) nofile:1: unexpected token: end")
  end
end

defmodule Kernel.CLI.CompileTest do
  use ExUnit.Case, async: true

  test :compile_code do
    fixture = fixture_path "compile_sample.ex"
    assert elixirc('#{fixture} -o #{tmp_path}') == ''
    assert File.regular?(tmp_path "Elixir.CompileSample.beam")
  after
    File.rm(tmp_path("Elixir.CompileSample.beam"))
  end

  test :compile_code_verbose do
    fixture = fixture_path "compile_sample.ex"
    assert elixirc('#{fixture} -o #{tmp_path} --verbose') ==
      'Compiled #{fixture}\n'
    assert File.regular?(tmp_path "Elixir.CompileSample.beam")
  after
    File.rm(tmp_path("Elixir.CompileSample.beam"))
  end

  test :possible_deadlock do
    output = elixirc('#{fixture_path("parallel_deadlock")} -o #{tmp_path}')
    foo = '* #{fixture_path "parallel_deadlock/foo.ex"} is missing module Bar'
    bar = '* #{fixture_path "parallel_deadlock/bar.ex"} is missing module Foo'
    assert :string.str(output, foo) > 0, "expected foo.ex to miss module Bar"
    assert :string.str(output, bar) > 0, "expected bar.ex to miss module Foo"
    assert :string.str(output, 'elixir_compiler') == 0, "expected elixir_compiler to not be in output"
  end
end

defmodule Kernel.CLI.ParallelCompilerTest do
  use ExUnit.Case
  import ExUnit.CaptureIO

  test :files do
    fixtures = [fixture_path("parallel_compiler/bar.ex"), fixture_path("parallel_compiler/foo.ex")]
    assert capture_io(fn ->
      assert [Bar, Foo] = Kernel.ParallelCompiler.files fixtures
    end) =~ "message_from_foo"
  end

  test :warnings_as_errors do
    warnings_as_errors = Code.compiler_options[:warnings_as_errors]

    try do
      Code.compiler_options(warnings_as_errors: true)

      assert_raise CompileError, fn ->
        capture_io :stderr, fn ->
          Kernel.ParallelCompiler.files [fixture_path("warnings_sample.ex")]
        end
      end
    after
      Code.compiler_options(warnings_as_errors: warnings_as_errors)
    end
  end
end
<commit_msg>CLI/Compile: added a test to showcase the new behaviour<commit_after>Code.require_file "../test_helper.exs", __DIR__

import PathHelpers

defmodule Kernel.CLI.InitTest do
  use ExUnit.Case, async: true

  test :code_init do
    assert elixir('-e "IO.puts [?3]"') == '3\n'

    result = elixir('-e "IO.puts inspect(System.argv)" #{fixture_path("init_sample.exs")} -o 1 2 3')
    assert result == '#{inspect ["-o", "1", "2", "3"]}\n3\n'
  end
end

defmodule Kernel.CLI.OptionParsingTest do
  use ExUnit.Case, async: true

  test :path do
    root = fixture_path("../../..") |> to_char_list
    list = elixir('-pa "#{root}/*" -pz "#{root}/lib/*" -e "IO.inspect(:code.get_path, limit: :infinity)"')
    { path, _ } = Code.eval_string list, []

    # pa
    assert Path.expand('ebin', root) in path
    assert Path.expand('lib', root) in path
    assert Path.expand('src', root) in path

    # pz
    assert Path.expand('lib/list', root) in path
  end
end

defmodule Kernel.CLI.AtExitTest do
  use ExUnit.Case, async: true

  test :at_exit do
    assert elixir(fixture_path("at_exit.exs") |> to_char_list) ==
      'goodbye cruel world with status 0\n'
  end
end

defmodule Kernel.CLI.ErrorTest do
  use ExUnit.Case, async: true

  test :code_error do
    assert :string.str('** (throw) 1', elixir('-e "throw 1"')) == 0
    assert :string.str('** (ErlangError) erlang error: 1', elixir('-e "error 1"')) == 0

    # It does not catch exits with integers nor strings...
    assert elixir('-e "exit 1"') == ''
  end
end

defmodule Kernel.CLI.SyntaxErrorTest do
  use ExUnit.Case, async: true

  defp check_output(elixir_cmd, expected_msg) do
    o = elixir(elixir_cmd)
    assert :string.str(o, expected_msg) == 1, "Expected this output: `#{expected_msg}`\nbut got this output: `#{o}`"
  end

  test :syntax_code_error do
    check_output('-e "[1,2"', '** (TokenMissingError) nofile:1: missing terminator: ]')
    check_output('-e "case 1 end"', %C"** (SyntaxError) nofile:1: unexpected token: end")
  end
end

defmodule Kernel.CLI.CompileTest do
  use ExUnit.Case, async: true

  test :compile_code do
    fixture = fixture_path "compile_sample.ex"
    assert elixirc('#{fixture} -o #{tmp_path}') == ''
    assert File.regular?(tmp_path "Elixir.CompileSample.beam")
  after
    File.rm(tmp_path("Elixir.CompileSample.beam"))
  end

  test :compile_code_verbose do
    fixture = fixture_path "compile_sample.ex"
    assert elixirc('#{fixture} -o #{tmp_path} --verbose') ==
      'Compiled #{fixture}\n'
    assert File.regular?(tmp_path "Elixir.CompileSample.beam")
  after
    File.rm(tmp_path("Elixir.CompileSample.beam"))
  end

  test :possible_deadlock do
    output = elixirc('#{fixture_path("parallel_deadlock")} -o #{tmp_path}')
    foo = '* #{fixture_path "parallel_deadlock/foo.ex"} is missing module Bar'
    bar = '* #{fixture_path "parallel_deadlock/bar.ex"} is missing module Foo'
    assert :string.str(output, foo) > 0, "expected foo.ex to miss module Bar"
    assert :string.str(output, bar) > 0, "expected bar.ex to miss module Foo"
    assert :string.str(output, 'elixir_compiler') == 0, "expected elixir_compiler to not be in output"
  end

  test :compile_missing_patterns do
    fixture = fixture_path "compile_sample.ex"
    output = elixirc('#{fixture} non_existing.ex -o #{tmp_path}')
    assert :string.str(output, 'non_existing.ex') > 0, "expected non_existing.ex to be mentionned"
    assert :string.str(output, 'compile_sample.ex') == 0, "expected compile_sample.ex to not be mentionned"
    refute File.exists?(tmp_path("Elixir.CompileSample.beam")) , "expected the sample to not be compiled"
  end
end

defmodule Kernel.CLI.ParallelCompilerTest do
  use ExUnit.Case
  import ExUnit.CaptureIO

  test :files do
    fixtures = [fixture_path("parallel_compiler/bar.ex"), fixture_path("parallel_compiler/foo.ex")]
    assert capture_io(fn ->
      assert [Bar, Foo] = Kernel.ParallelCompiler.files fixtures
    end) =~ "message_from_foo"
  end

  test :warnings_as_errors do
    warnings_as_errors = Code.compiler_options[:warnings_as_errors]

    try do
      Code.compiler_options(warnings_as_errors: true)

      assert_raise CompileError, fn ->
        capture_io :stderr, fn ->
          Kernel.ParallelCompiler.files [fixture_path("warnings_sample.ex")]
        end
      end
    after
      Code.compiler_options(warnings_as_errors: warnings_as_errors)
    end
  end
end

{'fraction_non_alphanumeric': 0.13811420982735723, 'fraction_numerical': 0.0065193770373053244, 'mean_word_length': 3.7337142857142855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, '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': '4217837', 'n_tokens_mistral': 3000, 'n_tokens_neox': 2905, 'n_words': 732}