Config-Resolver

 view release on metacpan or  search on metacpan

t/09_write_output.t  view on Meta::CPAN

};

# --- Test 3: Hash to STDOUT (pretty JSON) ---
subtest 'Test Case: Hash to STDOUT (pretty JSON)' => sub {
  $mock->{_mock_outfile} = undef;
  $mock->{_mock_format}  = 'json';
  $mock->{_mock_pretty}  = 1;      # Turn on pretty

  my $hash_data     = { a => 1, b => 2 };
  my $expected_json = to_json( $hash_data, { pretty => 1, allow_blessed => 1, convert_blessed => 1 } );

  my $output = capture_stdout(
    sub {
      $mock->write_output($hash_data);
    }
  );

  is( $output, $expected_json, 'Correctly prints pretty JSON to STDOUT' );
};

# --- Test 4: Hash to STDOUT (YAML) ---
subtest 'Test Case: Hash to STDOUT (YAML)' => sub {
  $mock->{_mock_outfile} = undef;
  $mock->{_mock_format}  = 'yml';

  my $hash_data     = { a => 1, b => 2 };
  my $expected_yaml = Dump($hash_data);

  my $output = capture_stdout(
    sub {
      $mock->write_output($hash_data);
    }
  );

  is( $output, $expected_yaml, 'Correctly prints YAML to STDOUT' );
};

# --- Test 5: Hash to file (with umask) ---
subtest 'Test Case: Hash to file with umask' => sub {

  # 1. Create a temp *filename*, but not the file itself
  # UNLINK => 1 means it will be deleted after the test
  my ( undef, $temp_file ) = tempfile( UNLINK => 1 );

  $mock->{_mock_outfile} = $temp_file;
  $mock->{_mock_format}  = 'json';
  $mock->{_mock_pretty}  = 0;
  $mock->{_mock_umask}   = '0077';     # Set a very restrictive umask

  my $hash_data     = { a => 1, b => 2 };
  my $expected_json = to_json( $hash_data, { pretty => 0, allow_blessed => 1, convert_blessed => 1 } );

  # 2. Run the sub -- this will create the file
  $mock->write_output($hash_data);

  # 3. Test the file's *content*
  my $file_content = slurp_file($temp_file);
  is( $file_content, $expected_json, 'File content is correct' );

  # 4. Test the file's *permissions*
  # (stat($file))[2] returns the mode
  my $mode = ( stat($temp_file) )[2] & 0777;
  # A umask of 0077 on a file (default 0666) should be 0600
  my $expected_mode = 0600;  # (rw-------)

  is( $mode, $expected_mode, 'File permissions are correctly set by umask 0077' );
};

done_testing();

1;



( run in 1.611 second using v1.01-cache-2.11-cpan-39bf76dae61 )