App-karr

 view release on metacpan or  search on metacpan

t/71-legacy-encoding-repair.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use lib 't/lib';
use TestGit qw( require_git_c );
require_git_c();
use TestKarr qw( run_karr );
use File::Temp qw( tempdir );
use YAML::XS ();
use JSON::MaybeXS qw( encode_json decode_json );
use Encode qw( encode_utf8 decode );

use App::karr::Git;
use App::karr::Encoding qw( BOARD_ENCODING_VERSION );

# Ticket #53, the half that is about boards that already exist.
#
# karr up to 0.402 fed YAML::XS::Dump the *octets* of every frontmatter value
# (because @ARGV was never decoded) and Dump encoded them a second time, so
# every board written by those versions carries double-encoded UTF-8 in its task
# frontmatter, its config, and its activity log. Task bodies do not: they were
# concatenated onto the document verbatim and are singly encoded. Fixing the
# encoding without accounting for that would have turned every existing board's
# titles into visible mojibake.
#
# The decision, and what this file pins:
#
#   * refs/karr/meta/encoding is the discriminator. Absent => a board written
#     by 0.402 or earlier,
#     read through App::karr::Encoding::repair_mojibake. Present and >= 2 =>
#     hands off, no guessing, forever.
#   * `karr init` and `karr import --yes` stamp it; `karr repair --yes` migrates
#     an old board and stamps it.
#   * The repair is idempotent and never touches a ref whose payload is ASCII.
#
# The legacy fixtures below are not an approximation. _legacy_task_doc was
# checked byte-for-byte against what karr 0.402 actually wrote for the same
# input before this test was committed.

# Failure diagnostics compare character strings; without this they would warn
# "Wide character in print" and render the mojibake and the repaired text
# identically, which is exactly the confusion this file exists to resolve.
binmode( Test::More->builder->$_, ':encoding(UTF-8)' )
  for qw( output failure_output todo_output );

my $TITLE = "Legacy \x{dc}nicode \x{2014} task";
my $BODY  = "Body caf\x{e9} \x{2014} na\x{ef}ve";
my $TAG   = "gr\x{fc}n";
my $NAME  = "Legacy B\x{f6}";

# In-process runner (t/lib/TestKarr.pm): same ($cwd, @argv) signature and
# { exit, stdout, stderr } return as the open3 helper this file used to carry,
# dispatched through the shared App::karr::Dispatch path. KARR_TEST_SUBPROC=1
# restores the old open3 path.
sub _run_karr { return run_karr(@_) }

sub _init_repo {
  my $repo = tempdir( CLEANUP => 1 );
  system( 'git', 'init', '-q', $repo );
  system( 'git', '-C', $repo, 'config', 'user.email', 'test@example.com' );
  system( 'git', '-C', $repo, 'config', 'user.name', 'Test User' );
  return $repo;
}

sub _blob {
  my ( $repo, $ref ) = @_;
  open my $fh, '-|', 'git', '-C', $repo, 'cat-file', '-p', "$ref:data" or die "git cat-file: $!";
  binmode $fh;
  my $raw = do { local $/; <$fh> };
  close $fh;
  return defined $raw ? $raw : '';
}

sub _oid {
  my ( $repo, $ref ) = @_;
  my $out = `git -C $repo rev-parse $ref 2>/dev/null`;
  chomp $out;
  return $out;
}



( run in 0.923 second using v1.01-cache-2.11-cpan-364913b4093 )