github.com/google/yamlfmt@v0.12.2-0.20240514121411-7f77800e2681/integrationtest/command/command_test.go (about)

     1  // Copyright 2024 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build integration_test
    16  
    17  package command
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  )
    25  
    26  var (
    27  	updateFlag *bool = flag.Bool("update", false, "Whether to update the goldens.")
    28  	stdoutFlag *bool = flag.Bool("stdout", false, "Show stdout instead of diffing it.")
    29  	yamlfmtBin string
    30  )
    31  
    32  func TestMain(m *testing.M) {
    33  	yamlfmtBinVar := os.Getenv("YAMLFMT_BIN")
    34  	if yamlfmtBinVar == "" {
    35  		fmt.Println("Must provide a YAMLFMT_BIN environment variable.")
    36  		os.Exit(1)
    37  	}
    38  	yamlfmtBin = yamlfmtBinVar
    39  	m.Run()
    40  }
    41  
    42  func yamlfmtWithArgs(args string) string {
    43  	return fmt.Sprintf("%s -no_global_conf %s", yamlfmtBin, args)
    44  }
    45  
    46  func TestPathArg(t *testing.T) {
    47  	TestCase{
    48  		Dir:     "path_arg",
    49  		Command: yamlfmtWithArgs("x.yaml"),
    50  		Update:  *updateFlag,
    51  	}.Run(t)
    52  }
    53  
    54  func TestIncludeDocumentStart(t *testing.T) {
    55  	TestCase{
    56  		Dir:     "include_document_start",
    57  		Command: yamlfmtWithArgs("-formatter include_document_start=true x.yaml"),
    58  		Update:  *updateFlag,
    59  	}.Run(t)
    60  }
    61  
    62  func TestGitignore(t *testing.T) {
    63  	TestCase{
    64  		Dir:     "gitignore",
    65  		Command: yamlfmtWithArgs("-gitignore_excludes -gitignore_path .test_gitignore ."),
    66  		Update:  *updateFlag,
    67  	}.Run(t)
    68  }
    69  
    70  func TestLint(t *testing.T) {
    71  	TestCase{
    72  		Dir:     "lint",
    73  		Command: yamlfmtWithArgs("-lint ."),
    74  		Update:  *updateFlag,
    75  		IsError: true,
    76  	}.Run(t)
    77  }
    78  
    79  func TestLineOutput(t *testing.T) {
    80  	TestCase{
    81  		Dir:     "line_output",
    82  		Command: yamlfmtWithArgs("-lint -output_format line ."),
    83  		Update:  *updateFlag,
    84  		IsError: true,
    85  	}.Run(t)
    86  }
    87  
    88  func TestDry(t *testing.T) {
    89  	TestCase{
    90  		Dir:     "dry",
    91  		Command: yamlfmtWithArgs("-dry ."),
    92  		Update:  *updateFlag,
    93  	}.Run(t)
    94  }
    95  
    96  func TestDryQuiet(t *testing.T) {
    97  	TestCase{
    98  		Dir:     "dry_quiet",
    99  		Command: yamlfmtWithArgs("-dry -quiet ."),
   100  		Update:  *updateFlag,
   101  	}.Run(t)
   102  }
   103  
   104  func TestPrintConfFlags(t *testing.T) {
   105  	TestCase{
   106  		Dir:     "print_conf_flags",
   107  		Command: yamlfmtWithArgs("-print_conf -continue_on_error=true -formatter retain_line_breaks=true"),
   108  		Update:  *updateFlag,
   109  	}.Run(t)
   110  }
   111  
   112  func TestPrintConfFile(t *testing.T) {
   113  	TestCase{
   114  		Dir:     "print_conf_file",
   115  		Command: yamlfmtWithArgs("-print_conf"),
   116  		Update:  *updateFlag,
   117  	}.Run(t)
   118  }
   119  func TestPrintConfFlagsAndFile(t *testing.T) {
   120  	TestCase{
   121  		Dir:     "print_conf_flags_and_file",
   122  		Command: yamlfmtWithArgs("-print_conf -continue_on_error=true -formatter retain_line_breaks=true"),
   123  		Update:  *updateFlag,
   124  	}.Run(t)
   125  }