github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/man/man_test.go (about)

     1  // Copyright 2019 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  package man_test
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	. "github.com/GoogleContainerTools/kpt/internal/util/man"
    25  	kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  // TestMan_Execute verifies that Execute will find the man page file,
    30  // format it as a man page, and execute a command to display it.
    31  func TestMan_Execute(t *testing.T) {
    32  	d := t.TempDir()
    33  
    34  	// write the KptFile
    35  	err := os.WriteFile(filepath.Join(d, kptfilev1.KptFileName), []byte(`
    36  apiVersion: kpt.dev/v1
    37  kind: Kptfile
    38  metadata:
    39    name: java
    40  info:
    41    man: "man/README.md"
    42  `), 0600)
    43  	assert.NoError(t, err)
    44  
    45  	// write the man md file
    46  	err = os.Mkdir(filepath.Join(d, "man"), 0700)
    47  	assert.NoError(t, err)
    48  	err = os.WriteFile(filepath.Join(d, "man", ManFilename), []byte(`
    49  java 1   "June 2019"  "Application"
    50  ==================================================
    51  
    52  # NAME
    53    **java**
    54  
    55  # SYNOPSIS
    56  
    57  kpt clone testdata3/java
    58  
    59  # Description
    60  
    61  The **java** package runs a container containing a java application.
    62  
    63  # Components
    64  
    65  Java server Deployment.
    66  
    67      apiVersion: apps/v1
    68      kind: Deployment
    69      metadata:
    70        name: java
    71  
    72  Java server Service
    73  
    74      apiVersion: v1
    75      kind: Service
    76      metadata:
    77        name: java
    78  
    79  Java server ConfigMap
    80  
    81      apiVersion: v1
    82      kind: ConfigMap
    83      metadata:
    84        name: java-config
    85  `), 0600)
    86  	assert.NoError(t, err)
    87  
    88  	b := &bytes.Buffer{}
    89  	instance := Command{
    90  		ManExecCommand: "cat",
    91  		Path:           d,
    92  		StdOut:         b,
    93  	}
    94  	err = instance.Run()
    95  	assert.NoError(t, err)
    96  
    97  	assert.Equal(t, `.nh
    98  .TH java 1   "June 2019"  "Application"
    99  
   100  .SH NAME
   101  .PP
   102  \fBjava\fP
   103  
   104  
   105  .SH SYNOPSIS
   106  .PP
   107  kpt clone testdata3/java
   108  
   109  
   110  .SH Description
   111  .PP
   112  The \fBjava\fP package runs a container containing a java application.
   113  
   114  
   115  .SH Components
   116  .PP
   117  Java server Deployment.
   118  
   119  .PP
   120  .RS
   121  
   122  .nf
   123  apiVersion: apps/v1
   124  kind: Deployment
   125  metadata:
   126    name: java
   127  
   128  .fi
   129  .RE
   130  
   131  .PP
   132  Java server Service
   133  
   134  .PP
   135  .RS
   136  
   137  .nf
   138  apiVersion: v1
   139  kind: Service
   140  metadata:
   141    name: java
   142  
   143  .fi
   144  .RE
   145  
   146  .PP
   147  Java server ConfigMap
   148  
   149  .PP
   150  .RS
   151  
   152  .nf
   153  apiVersion: v1
   154  kind: ConfigMap
   155  metadata:
   156    name: java-config
   157  
   158  .fi
   159  .RE
   160  `, b.String())
   161  }
   162  
   163  // TestMan_GetExecCmd tests that the exec command is defaulted to "man",
   164  // but can be overridden
   165  func TestMan_GetExecCmd(t *testing.T) {
   166  	// default to "man"
   167  	instance := Command{}
   168  	assert.Equal(t, "man", instance.GetExecCmd())
   169  
   170  	// allow overrides for testing
   171  	instance = Command{ManExecCommand: "cat"}
   172  	assert.Equal(t, "cat", instance.GetExecCmd())
   173  }
   174  
   175  // TestMan_GetStdOut tests that the command stdout is defaulted to "os.Stdout",
   176  // but can be overridden.
   177  func TestMan_GetStdOut(t *testing.T) {
   178  	// default to stdout
   179  	instance := Command{}
   180  	assert.Equal(t, os.Stdout, instance.GetStdOut())
   181  
   182  	// allow overrides for testing
   183  	b := &bytes.Buffer{}
   184  	instance = Command{StdOut: b}
   185  	assert.Equal(t, b, instance.GetStdOut())
   186  }
   187  
   188  // TestMan_Execute_failNoManPage verifies that if the man page is not
   189  // specified for the package, an error is returned.
   190  func TestMan_Execute_failNoManPage(t *testing.T) {
   191  	d := t.TempDir()
   192  
   193  	// write the KptFile
   194  	err := os.WriteFile(filepath.Join(d, kptfilev1.KptFileName), []byte(`
   195  apiVersion: kpt.dev/v1
   196  kind: Kptfile
   197  metadata:
   198    name: java
   199  info:
   200  `), 0600)
   201  	if !assert.NoError(t, err) {
   202  		return
   203  	}
   204  
   205  	b := &bytes.Buffer{}
   206  	instance := Command{
   207  		ManExecCommand: "cat",
   208  		Path:           d,
   209  		StdOut:         b,
   210  	}
   211  	err = instance.Run()
   212  	if !assert.EqualError(t, err, fmt.Sprintf("no manual entry for %q", d)) {
   213  		return
   214  	}
   215  	if !assert.Equal(t, ``, b.String()) {
   216  		return
   217  	}
   218  }
   219  
   220  // TestMan_Execute_failBadPath verifies that Execute will fail if the man
   221  // path does not exist.
   222  func TestMan_Execute_failBadPath(t *testing.T) {
   223  	d := t.TempDir()
   224  
   225  	// write the KptFile
   226  	err := os.WriteFile(filepath.Join(d, kptfilev1.KptFileName), []byte(`
   227  apiVersion: kpt.dev/v1
   228  kind: Kptfile
   229  metadata:
   230    name: java
   231  info:
   232    man: "not/real/path"
   233  `), 0600)
   234  	assert.NoError(t, err)
   235  
   236  	b := &bytes.Buffer{}
   237  	instance := Command{
   238  		ManExecCommand: "cat",
   239  		Path:           d,
   240  		StdOut:         b,
   241  	}
   242  	err = instance.Run()
   243  	assert.NotNil(t, err)
   244  	assert.Contains(t, err.Error(), "no such file or directory")
   245  	assert.Equal(t, ``, b.String())
   246  }
   247  
   248  // TestMan_Execute_failLocation verifies that Execute will fail if the man
   249  // path is not under the package directory.
   250  func TestMan_Execute_failLocation(t *testing.T) {
   251  	d := t.TempDir()
   252  
   253  	// write the KptFile
   254  	err := os.WriteFile(filepath.Join(d, kptfilev1.KptFileName), []byte(`
   255  apiVersion: kpt.dev/v1
   256  kind: Kptfile
   257  metadata:
   258    name: java
   259  info:
   260    man: "../../path"
   261  `), 0600)
   262  	assert.NoError(t, err)
   263  
   264  	b := &bytes.Buffer{}
   265  	instance := Command{
   266  		ManExecCommand: "cat",
   267  		Path:           d,
   268  		StdOut:         b,
   269  	}
   270  	err = instance.Run()
   271  	assert.EqualError(t, err, fmt.Sprintf("invalid manual location for %q", d))
   272  	assert.Equal(t, ``, b.String())
   273  }
   274  
   275  // TestMan_Execute_failLocation verifies that Execute will fail if the man
   276  // path is not under the package directory.
   277  func TestMan_Execute_failManNotInstalled(t *testing.T) {
   278  	b := &bytes.Buffer{}
   279  	instance := Command{
   280  		ManExecCommand: "notrealprogram",
   281  		Path:           "path",
   282  		StdOut:         b,
   283  	}
   284  	err := instance.Run()
   285  	assert.EqualError(t, err, "notrealprogram not installed")
   286  	assert.Equal(t, ``, b.String())
   287  }