github.com/wheelercj/pm2md@v0.0.11/cmd/utils_test.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  	"reflect"
    21  	"testing"
    22  )
    23  
    24  // assertPanic takes any function and arguments for that function, calls the given
    25  // function with the given arguments, and asserts that the given function then panics.
    26  func assertPanic(t *testing.T, f any, args ...any) {
    27  	defer func() {
    28  		if r := recover(); r == nil {
    29  			t.Errorf("panic expected")
    30  		}
    31  	}()
    32  
    33  	reflectArgs := make([]reflect.Value, len(args))
    34  	for i, arg := range args {
    35  		reflectArgs[i] = reflect.ValueOf(arg)
    36  	}
    37  
    38  	reflect.ValueOf(f).Call(reflectArgs)
    39  }
    40  
    41  func TestFileExists(t *testing.T) {
    42  	if !FileExists("../LICENSE") {
    43  		t.Error("FileExists(\"../LICENSE\") = false, want true")
    44  	}
    45  }
    46  
    47  func TestFileDoesNotExist(t *testing.T) {
    48  	if FileExists("nonexistent-file") {
    49  		t.Error("FileExists(\"nonexistent-file\") = true, want false")
    50  	}
    51  }
    52  
    53  func TestCreateUniqueFileName(t *testing.T) {
    54  	tests := []struct {
    55  		name, ext, want string
    56  	}{
    57  		{"../LICENSE", "", "../LICENSE.1"},
    58  		{"../README", ".md", "../README.1.md"},
    59  		{"nonexistent-file", ".txt", "nonexistent-file.txt"},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		testName := fmt.Sprintf("%q,%q", test.name, test.ext)
    64  		t.Run(testName, func(t *testing.T) {
    65  			ans := CreateUniqueFileName(test.name, test.ext)
    66  			if ans != test.want {
    67  				t.Errorf(
    68  					"CreateUniqueFileName(%q, %q) = %q, want %q",
    69  					test.name, test.ext, ans, test.want,
    70  				)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestCreateUniqueFileNamePanic(t *testing.T) {
    77  	tests := []struct {
    78  		name, ext string
    79  	}{
    80  		{"../README", "md"},
    81  		{"nonexistent-file", "."},
    82  		{"nonexistent-file", "a"},
    83  		{"", ""},
    84  		{"", ".md"},
    85  	}
    86  
    87  	for _, test := range tests {
    88  		testName := fmt.Sprintf("%q,%q", test.name, test.ext)
    89  		t.Run(testName, func(t *testing.T) {
    90  			assertPanic(t, CreateUniqueFileName, test.name, test.ext)
    91  		})
    92  	}
    93  }
    94  
    95  func TestFormatFileName(t *testing.T) {
    96  	tests := []struct {
    97  		name, input, want string
    98  	}{
    99  		{"spaces", "file name with spaces", "file-name-with-spaces"},
   100  		{"special characters", "lots-of-#<>$+%&/\\*|{}!?`'\"=:@-special-characters", "lots-of-----------------------special-characters"},
   101  		{"invalid start and end", ".  invalid-start-and-end__--", "invalid-start-and-end"},
   102  	}
   103  
   104  	for _, test := range tests {
   105  		t.Run(test.name, func(t *testing.T) {
   106  			ans := FormatFileName(test.input)
   107  			if ans != test.want {
   108  				t.Errorf("FormatFileName(%q) = %q, want %q", test.input, ans, test.want)
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func TestExportText(t *testing.T) {
   115  	// This test needs to create default.tmpl but default.tmpl already exists in this
   116  	// directory, so the current directory needs to temporarily change.
   117  	err := os.Chdir("..")
   118  	if err != nil {
   119  		t.Error(err)
   120  		return
   121  	}
   122  	if FileExists("default.tmpl") {
   123  		t.Errorf("FileExists(\"default.tmpl\") = true, want false")
   124  		return
   125  	}
   126  	fileName := exportText("default", ".tmpl", defaultTmplStr)
   127  	if fileName != "default.tmpl" {
   128  		t.Errorf("exportDefaultTemplate() = %q, want \"default.tmpl\"", fileName)
   129  	}
   130  	if !FileExists(fileName) {
   131  		t.Errorf("FileExists(%q) = false, want true", fileName)
   132  	}
   133  	os.Remove(fileName)
   134  	err = os.Chdir("cmd")
   135  	if err != nil {
   136  		t.Error(err)
   137  		return
   138  	}
   139  }