github.com/wheelercj/pm2md@v0.0.11/cmd/test_cmd.go (about)

     1  // Copyright 2023 Chris Wheeler
     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  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"path"
    21  	"strings"
    22  
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  var testCmd = &cobra.Command{
    27  	Use:   "test [api.json custom.tmpl expected.md]",
    28  	Short: "Test your custom template with expected output",
    29  	Args:  testArgsFunc,
    30  	RunE:  testRunFunc,
    31  }
    32  
    33  // testArgsFunc does some input validation on the `test` subcommand's args and flags.
    34  func testArgsFunc(cmd *cobra.Command, args []string) error {
    35  	if len(CustomTmplPath) > 0 {
    36  		return fmt.Errorf("with the test subcommand, choose a custom template without using the flag")
    37  	}
    38  	if err := cobra.ExactArgs(3)(cmd, args); err != nil {
    39  		return err
    40  	}
    41  	if !strings.HasSuffix(strings.ToLower(args[0]), ".json") {
    42  		return fmt.Errorf("%q must end with \".json\"", args[0])
    43  	}
    44  	if !strings.HasSuffix(strings.ToLower(args[1]), ".tmpl") {
    45  		return fmt.Errorf("%q must end with \".tmpl\"", args[1])
    46  	}
    47  	return nil
    48  }
    49  
    50  // testRunFunc parses the `test` subcommand's args and flags, and asserts the given JSON
    51  // and template result in the given plaintext.
    52  func testRunFunc(cmd *cobra.Command, args []string) error {
    53  	jsonPath := args[0]
    54  	tmplPath := args[1]
    55  	wantPath := args[2]
    56  
    57  	statusRanges, err := parseStatusRanges(Statuses)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	err = AssertGenerateNoDiff(jsonPath, tmplPath, wantPath, statusRanges)
    63  	if err == nil {
    64  		fmt.Fprintf(os.Stderr, "Perfect match!")
    65  	} else {
    66  		fmt.Fprint(os.Stderr, fmt.Sprint(err))
    67  	}
    68  
    69  	return nil
    70  }
    71  
    72  // loadTmpl loads a template's name and the template itself into strings. If the given
    73  // template path is empty, the default template is used.
    74  func loadTmpl(tmplPath string) (tmplName string, tmplStr string, err error) {
    75  	if len(tmplPath) > 0 {
    76  		tmplBytes, err := os.ReadFile(tmplPath)
    77  		if err != nil {
    78  			return "", "", err
    79  		}
    80  		tmplStr = string(tmplBytes)
    81  		tmplName = path.Base(strings.ReplaceAll(tmplPath, "\\", "/"))
    82  	} else {
    83  		tmplStr = defaultTmplStr
    84  		tmplName = defaultTmplName
    85  	}
    86  
    87  	return tmplName, tmplStr, nil
    88  }