# frozen_string_literal: true require "English" require_relative "../test_helper" require_relative "../snapshot_utils" require "tempfile" require "json" module Engine class CLIStdinTest < Minitest::Spec include SnapshotUtils def setup skip "Shell stdin tests are skipped in CI" if ENV["CI"] end def with_temp_file(content) file = Tempfile.new(["test_template", ".erb"]) file.write(content) file.close yield file.path ensure file&.unlink end test "shell: lex accepts piped stdin" do output = `echo '
Hello
' | exe/herb lex` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-0") end test "shell: parse accepts piped stdin" do output = `echo '
Hello
' | exe/herb parse` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-1") end test "shell: compile accepts piped stdin" do output = `echo '
<%= name %>
' | exe/herb compile` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-2") end test "shell: ruby accepts piped stdin" do output = `echo '
<%= user.name %>
' | exe/herb ruby` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-3") end test "shell: html accepts piped stdin" do output = `echo '
<%= user.name %>
' | exe/herb html` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-4") end test "shell: render accepts piped stdin" do output = `echo '
Static
' | exe/herb render` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-5") end test "shell: compile with json flag and stdin" do output = `echo '
Test
' | exe/herb compile --json` assert $CHILD_STATUS.success?, "Command failed with: #{output}" json_data = JSON.parse(output) assert_equal true, json_data["success"] assert_snapshot_matches(json_data["source"], "cli_stdin_test-6") end test "shell: lex with json flag and stdin" do output = `echo '
Test
' | exe/herb lex --json` assert $CHILD_STATUS.success?, "Command failed with: #{output}" json_data = JSON.parse(output) assert_kind_of Array, json_data assert(json_data.any? { |token| token["type"] == "TOKEN_HTML_TAG_START" }) end test "shell: compile with multiple flags and stdin" do output = `echo '
<%= x %>
' | exe/herb compile --no-escape --freeze` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-7") end test "shell: stdin with file redirection and dash" do with_temp_file("
Content
") do |file_path| output = `exe/herb lex - < #{file_path}` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-8") end end test "shell: cat pipe to lex" do with_temp_file("
Data
") do |file_path| output = `cat #{file_path} | exe/herb lex` assert $CHILD_STATUS.success?, "Command failed with: #{output}" assert_snapshot_matches(output, "cli_stdin_test-9") end end end end