Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 3 commits
  • 5 files changed
  • 0 commit comments
  • 1 contributor
Commits on Mar 21, 2012
@rwstauner Mark encoding tests as TODO tests
they are vulnerable to ENV and i need to understand more
464b88f
@rwstauner Use vim script during make_pl to detect version a2df22d
@rwstauner v0.16
  [Installation]
  - Simplify version detection using vim script

  [Testing]
  - Mark encoding tests as TODO tests for now (still under development)
fa2f125
Showing with 121 additions and 29 deletions.
  1. +8 −0 Changes
  2. +1 −1 README.pod
  3. +3 −0 dist.ini
  4. +89 −25 inc/VimCommand.pm
  5. +20 −3 t/encoding.t
View
8 Changes
@@ -2,6 +2,14 @@ Revision history for Text-VimColor
{{$NEXT}}
+0.16 2012-03-21T17:38:45Z
+
+ [Installation]
+ - Simplify version detection using vim script
+
+ [Testing]
+ - Mark encoding tests as TODO tests for now (still under development)
+
0.15 2012-03-18T23:43:36Z
[Performance]
View
2 README.pod
@@ -11,7 +11,7 @@ Text::VimColor - Syntax highlight text using Vim
=head1 VERSION
-version 0.15
+version 0.16
=head1 SYNOPSIS
View
3 dist.ini
@@ -7,6 +7,8 @@ copyright_holder = Randy Stauner
copyright_year = 2011
[@Author::RWSTAUNER]
+; declare prereqs used by inc/ even though they're currently all core modules
+AutoPrereqs.configure_finder = :IncModules
; current default, but very important. the lib/auto tree only exists to enable `prove -lv`
GatherDir.follow_symlinks = 0
@@ -18,6 +20,7 @@ Term::ANSIColor = 1.03
Term::ANSIColor = 3
[RemovePrereqs]
+; one of the tests uses this
remove = inc::VimCommand
; default GatherDir skips leading-dot dirs but we want to include this specific one
View
114 inc/VimCommand.pm
@@ -6,38 +6,93 @@ package inc::VimCommand;
#our $MIN = '5.4';
our $MIN = '6.0';
+use File::Temp qw( tempfile ); # core
+use IO::File; # core
use IPC::Open3 'open3'; # core
+# vim -h works at least as far back as 5.0 (--version not until 5.2)
+# first line: "VIM - Vi IMproved 5.0 (date)"
+our $VERSION_RE = qr/vim .+ ([0-9.]+)/i;
+
+our $INFO = {};
+
+our $MESSAGE = <<MESSAGE;
+This module requires vim version $MIN or later
+which must be compiled with at least the 'normal' feature set
+(--with-features=normal) particularly including +syntax (obviously).
+
+If you believe this to be an error please submit a bug report
+including the output of `vim --version` if possible.
+MESSAGE
+
sub import {
- require_minimum();
+ require_minimum_with_message();
+}
+
+sub require_minimum_with_message {
+ eval {
+ require_minimum();
+ };
+ my $msg = $@;
+ if( $msg ){
+ $msg .= "\n$MESSAGE\n";
+ $msg =~ s/^/# /mg;
+ die $msg;
+ }
}
-# TODO: die if `vim --version` =~ /-syntax/ ?
sub require_minimum {
- # vim -h works at least as far back as 5.0 (--version not until 5.2)
- my $output = vim('-h');
+ my $info = info_from_script();
+ my $ver = sprintf("%0.2f", $info->{version}/100);
- # first line: "VIM - Vi IMproved 5.0 (date)"
- if( my $v = ($output =~ /(\d+\.\d+)/)[0] ){
- if( $v < $MIN ){
- my $msg = <<MINVER;
-$output
+ if( !$ver ){
+ die "Unable to identify vim version ($ver)\n";
+ }
+ elsif( $ver < $MIN ){
+ $ver =~ s/\.0/./; # 5.08 => 5.8
+ die "Vim version $ver too low.\n";
+ }
+ elsif( !$info->{syntax} ){
+ die "Vim does not have the +syntax feature.\n";
+ }
+ return $ver;
+}
-This module requires version $MIN of vim.
-Version $v detected.
+sub info_from_script {
+ # touch output file
+ my $out = write_temp('tvc-out-XXXXXX', '');
-If you believe this to be in error please submit a bug report.
+ # NOTE: use single quotes, or backslashed double quotes in the '=' expr:
+ my $script = write_temp('tvc-script-XXXXXX', <<SCRIPT);
+:put ='vim:' . version . ',syn:' . has('syntax') . ','
+:0d
+:write! $out
+:quit!
+SCRIPT
-MINVER
- $msg =~ s/^/# /mg;
- die $msg;
- }
- }
- else {
- warn "Failed to parse vim version... More failures likely.\n"
- }
+ # try
+ eval {
+ # use IPC::Open3 to prevent STDOUT/ERR from interfering with make/test
+ vim(qw(-u NONE -s), $script);
- return;
+ my $output = do { local $/; IO::File->new($out, 'r')->getline; };
+
+ die "Vim script failed: Output file is empty.\n"
+ unless $output;
+ die "Failed to parse vim output:\n>$output\n"
+ unless $output =~ /^vim:(\d+),syn:(.+?),$/;
+
+ # if there was more info to get we could parse the output more generically
+ $INFO = { version => $1, syntax => $2 };
+ };
+ # catch part 1
+ my $e = $@;
+ # finally
+ unlink($out, $script);
+ # catch part 2
+ die $e if $e;
+
+ return $INFO;
}
sub vim {
@@ -56,12 +111,13 @@ sub vim {
my $pid = open3(my ($i, $o), undef, vim => @args);
waitpid($pid, 0);
- my $stat = $?;
+ #my $stat = $?;
alarm 0;
- # open3 probably died from this already
- die "Vim not found!\n" unless $stat >= 0;
+ # open3 will probably die if vim isn't found (in unix environments)
+ # we can't really trust the exit status to mean anything...
+ # vim might exit 1 or 2 (or 0) for -h/--version, cmd.exe exits 1 if not found...
local $/;
$output = <$o>;
@@ -69,9 +125,17 @@ sub vim {
if( my $e = $@ ){
$e = "Command aborted after $timeout seconds."
if $e eq "alarm\n";
- die "\n\nError attemting to execute 'vim':\n $e\n\n";
+ die "Error attemting to execute 'vim':\n $e\n";
}
return $output;
}
+sub write_temp {
+ my ($template, $text) = @_;
+ my ($fh, $path) = tempfile( $template, TMPDIR => 1 );
+ print $fh $text;
+ close $fh;
+ return $path;
+}
+
1;
View
23 t/encoding.t
@@ -9,6 +9,7 @@ BEGIN {
eval 'use Encode qw(encode decode); 1' # core in 5.7.3
or plan skip_all => 'Encode.pm is required for these tests';
}
+sub compare (&$$);
my $filetype = 'tvctestsyn';
# use high latin1 chars (but not utf8)
@@ -24,7 +25,7 @@ my $html =
# some of these use cases were taken from actual code in other CPAN modules.
# we'll test their usage here to ensure we don't break anything
-is nothing($filetype, "( hi )\n"),
+compare { nothing($filetype, "( hi )\n") }
qq[<span class="synSpecial">(</span>] .
qq[<span class="synComment"> hi </span>] .
qq[<span class="synSpecial">)</span>\n],
@@ -37,14 +38,30 @@ is nothing($filetype, "( hi )\n"),
# can we alter $input (en/decode or change the utf8 flag) and get a different result?
-is prepend_bom($filetype, $input), $html,
+compare { prepend_bom($filetype, $input) } $html,
'used BOM to get vim to honor encoded text';
-is pass_vim_options(undef, $input, {filetype => $filetype}), $html,
+compare { pass_vim_options(undef, $input, {filetype => $filetype}) } $html,
'specify encoding by adding "+set fenc=..." to vim_options';
done_testing;
+sub compare (&$$) {
+ my ($get, $exp, $name) = @_;
+
+ local @ENV{qw(LANG LC_ALL)} = ();
+ foreach my $env (
+ { LANG => 'en_US.UTF-8' },
+ { LC_ALL => 'C' },
+ ){
+ local @ENV{ keys %$env } = values %$env;
+ local $TODO = 'Encoding tests still under development';
+ my $desc = sprintf "%s (%s=%s)", $name, %$env;
+ my $got = $get->();
+ is $got, $exp, $desc;
+ }
+}
+
sub nothing {
my ($filetype, $input) = @_;
Text::VimColor->new(filetype => $filetype, string => $input)->html;

No commit comments for this range

Something went wrong with that request. Please try again.