|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative '../../helper' |
| 4 | + |
| 5 | +return if RUBY_DESCRIPTION =~ /truffleruby/ || RUBY_DESCRIPTION =~ /jruby/ |
| 6 | + |
| 7 | +begin |
| 8 | + require 'mini_racer' |
| 9 | +rescue LoadError |
| 10 | + return |
| 11 | +end |
| 12 | + |
| 13 | +class RDocGeneratorAlikiHighlightBashTest < Test::Unit::TestCase |
| 14 | + HIGHLIGHT_BASH_JS_PATH = File.expand_path( |
| 15 | + '../../../../lib/rdoc/generator/template/aliki/js/bash_highlighter.js', |
| 16 | + __dir__ |
| 17 | + ) |
| 18 | + |
| 19 | + HIGHLIGHT_BASH_JS = begin |
| 20 | + highlight_bash_js = File.read(HIGHLIGHT_BASH_JS_PATH) |
| 21 | + |
| 22 | + # We need to modify the JS slightly to make it work in the context of a test. |
| 23 | + highlight_bash_js.gsub( |
| 24 | + /\(function\(\) \{[\s\S]*'use strict';/, |
| 25 | + "// Test wrapper\n" |
| 26 | + ).gsub( |
| 27 | + /if \(document\.readyState[\s\S]*\}\)\(\);/, |
| 28 | + "// Removed DOM initialization for testing" |
| 29 | + ) |
| 30 | + end.freeze |
| 31 | + |
| 32 | + def setup |
| 33 | + @context = MiniRacer::Context.new |
| 34 | + @context.eval(HIGHLIGHT_BASH_JS) |
| 35 | + end |
| 36 | + |
| 37 | + def teardown |
| 38 | + @context.dispose |
| 39 | + end |
| 40 | + |
| 41 | + def test_prompts |
| 42 | + # $ followed by space or end of line is a prompt |
| 43 | + [ |
| 44 | + ['$ bundle exec rake', '<span class="sh-prompt">$</span>'], |
| 45 | + [' $ npm install', '<span class="sh-prompt">$</span>'], |
| 46 | + ['$', '<span class="sh-prompt">$</span>'], |
| 47 | + ].each do |input, expected| |
| 48 | + assert_includes highlight(input), expected, "Failed for: #{input}" |
| 49 | + end |
| 50 | + |
| 51 | + # $VAR is a variable, not a prompt |
| 52 | + refute_includes highlight('$HOME/bin'), '<span class="sh-prompt">' |
| 53 | + end |
| 54 | + |
| 55 | + def test_comments |
| 56 | + [ |
| 57 | + ['# This is a comment', '<span class="sh-comment"># This is a comment</span>'], |
| 58 | + ['bundle exec rake # Run tests', '<span class="sh-comment"># Run tests</span>'], |
| 59 | + ].each do |input, expected| |
| 60 | + assert_includes highlight(input), expected, "Failed for: #{input}" |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + def test_options |
| 65 | + [ |
| 66 | + ['ls -l', '<span class="sh-option">-l</span>'], |
| 67 | + ['npm install --save-dev', '<span class="sh-option">--save-dev</span>'], |
| 68 | + ['git commit --message=fix', '<span class="sh-option">--message=fix</span>'], |
| 69 | + ['ls -la --color=auto', ['<span class="sh-option">-la</span>', '<span class="sh-option">--color=auto</span>']], |
| 70 | + ].each do |input, expected| |
| 71 | + Array(expected).each do |exp| |
| 72 | + assert_includes highlight(input), exp, "Failed for: #{input}" |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + def test_strings |
| 78 | + [ |
| 79 | + ['echo "hello world"', '<span class="sh-string">"hello world"</span>'], |
| 80 | + ["echo 'hello world'", "<span class=\"sh-string\">'hello world'</span>"], |
| 81 | + ['echo "hello \"world\""', '<span class="sh-string">"hello \"world\""</span>'], |
| 82 | + ['npx @herb-tools/linter "**/*.rhtml"', '<span class="sh-string">"**/*.rhtml"</span>'], |
| 83 | + ].each do |input, expected| |
| 84 | + assert_includes highlight(input), expected, "Failed for: #{input}" |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + def test_commands |
| 89 | + result = highlight('bundle exec rake') |
| 90 | + assert_includes result, '<span class="sh-command">bundle</span>' |
| 91 | + # Only the first word is highlighted as command |
| 92 | + refute_includes result, '<span class="sh-command">exec</span>' |
| 93 | + refute_includes result, '<span class="sh-command">rake</span>' |
| 94 | + end |
| 95 | + |
| 96 | + def test_path_commands |
| 97 | + [ |
| 98 | + ['./configure --prefix=/usr/local', '<span class="sh-command">./configure</span>'], |
| 99 | + ['../configure --enable-gcov', '<span class="sh-command">../configure</span>'], |
| 100 | + ['./autogen.sh', '<span class="sh-command">./autogen.sh</span>'], |
| 101 | + ['~/.rubies/ruby-master/bin/ruby -e "puts 1"', '<span class="sh-command">~/.rubies/ruby-master/bin/ruby</span>'], |
| 102 | + ].each do |input, expected| |
| 103 | + assert_includes highlight(input), expected, "Failed for: #{input}" |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + def test_environment_variables |
| 108 | + [ |
| 109 | + ['COVERAGE=true make test', ['<span class="sh-envvar">COVERAGE=</span>', '<span class="sh-command">make</span>']], |
| 110 | + ['CC=clang CXX=clang++ make', ['<span class="sh-envvar">CC=</span>', '<span class="sh-envvar">CXX=</span>', '<span class="sh-command">make</span>']], |
| 111 | + ['RUBY_TEST_TIMEOUT_SCALE=5 make check', ['<span class="sh-envvar">RUBY_TEST_TIMEOUT_SCALE=</span>', '<span class="sh-command">make</span>']], |
| 112 | + ].each do |input, expected| |
| 113 | + Array(expected).each do |exp| |
| 114 | + assert_includes highlight(input), exp, "Failed for: #{input}" |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + def test_hyphens_in_words_not_options |
| 120 | + # Hyphen in @herb-tools/linter should NOT be treated as option |
| 121 | + result = highlight('npx @herb-tools/linter') |
| 122 | + assert_includes result, '<span class="sh-command">npx</span>' |
| 123 | + refute_includes result, '<span class="sh-option">-tools/linter</span>' |
| 124 | + assert_includes result, '@herb-tools/linter' |
| 125 | + |
| 126 | + # Command with hyphen gets highlighted as command, not option |
| 127 | + result = highlight('some-command arg') |
| 128 | + assert_includes result, '<span class="sh-command">some-command</span>' |
| 129 | + refute_includes result, '<span class="sh-option">' |
| 130 | + end |
| 131 | + |
| 132 | + def test_complex_commands |
| 133 | + # Typical shell command with prompt |
| 134 | + result = highlight('$ bundle exec rubocop -A') |
| 135 | + assert_includes result, '<span class="sh-prompt">$</span>' |
| 136 | + assert_includes result, '<span class="sh-command">bundle</span>' |
| 137 | + assert_includes result, '<span class="sh-option">-A</span>' |
| 138 | + |
| 139 | + # Complex git command |
| 140 | + result = highlight('$ git commit -m "Fix bug" --no-verify') |
| 141 | + assert_includes result, '<span class="sh-prompt">$</span>' |
| 142 | + assert_includes result, '<span class="sh-command">git</span>' |
| 143 | + assert_includes result, '<span class="sh-option">-m</span>' |
| 144 | + assert_includes result, '<span class="sh-string">"Fix bug"</span>' |
| 145 | + assert_includes result, '<span class="sh-option">--no-verify</span>' |
| 146 | + end |
| 147 | + |
| 148 | + def test_multiline_with_comments |
| 149 | + code = <<~SHELL |
| 150 | + # Generate documentation (creates _site directory) |
| 151 | + bundle exec rake rdoc |
| 152 | +
|
| 153 | + # Force regenerate documentation |
| 154 | + bundle exec rake rerdoc |
| 155 | + SHELL |
| 156 | + |
| 157 | + result = highlight(code) |
| 158 | + assert_includes result, '<span class="sh-comment"># Generate documentation (creates _site directory)</span>' |
| 159 | + assert_includes result, '<span class="sh-comment"># Force regenerate documentation</span>' |
| 160 | + end |
| 161 | + |
| 162 | + def test_empty_and_whitespace |
| 163 | + assert_equal '', highlight('') |
| 164 | + assert_equal " \n\t \n ", highlight(" \n\t \n ") |
| 165 | + end |
| 166 | + |
| 167 | + def test_html_escaping |
| 168 | + result = highlight('echo "<script>alert(1)</script>"') |
| 169 | + assert_includes result, '<script>' |
| 170 | + assert_includes result, '</script>' |
| 171 | + |
| 172 | + result = highlight('echo "a && b"') |
| 173 | + assert_includes result, '&&' |
| 174 | + end |
| 175 | + |
| 176 | + private |
| 177 | + |
| 178 | + def highlight(code) |
| 179 | + @context.eval("highlightShell(#{code.to_json})") |
| 180 | + end |
| 181 | +end |
0 commit comments