github.com/Mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/tools/gendoc_test.go (about)

     1  /*
     2  Copyright 2018 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tools
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	"github.com/Mirantis/virtlet/tests/gm"
    28  )
    29  
    30  func TestGenDocCommand(t *testing.T) {
    31  	tmpDir, err := ioutil.TempDir("", "gendoc-test")
    32  	if err != nil {
    33  		t.Fatalf("ioutil.TempDir(): %v", err)
    34  	}
    35  	defer os.RemoveAll(tmpDir)
    36  
    37  	cmd := NewGenDocCmd(fakeCobraCommand(), nil)
    38  	cmd.SilenceUsage = true
    39  	cmd.SilenceErrors = true
    40  	cmd.SetArgs([]string{tmpDir})
    41  	if err := cmd.Execute(); err != nil {
    42  		t.Errorf("Error running gendoc command: %v", err)
    43  	}
    44  
    45  	var buf bytes.Buffer
    46  	files, err := filepath.Glob(filepath.Join(tmpDir, "*"))
    47  	if err != nil {
    48  		t.Fatalf("Glob(): %v", err)
    49  	}
    50  	for _, p := range files {
    51  		if _, err := fmt.Fprintf(&buf, "# FILE: %s\n", filepath.Base(p)); err != nil {
    52  			t.Errorf("Error writing filename: %v", err)
    53  		}
    54  		if content, err := ioutil.ReadFile(p); err != nil {
    55  			t.Errorf("Can't read file %q: %v", p, err)
    56  		} else {
    57  			if _, err := fmt.Fprintf(&buf, "%s\n", content); err != nil {
    58  				t.Errorf("failed to write file content: %v", err)
    59  			}
    60  		}
    61  	}
    62  	gm.Verify(t, buf.Bytes())
    63  }
    64  
    65  func TestGenDocForConfig(t *testing.T) {
    66  	var buf bytes.Buffer
    67  	cmd := NewGenDocCmd(nil, &buf)
    68  	cmd.SilenceUsage = true
    69  	cmd.SilenceErrors = true
    70  	cmd.SetArgs([]string{"--config"})
    71  	if err := cmd.Execute(); err != nil {
    72  		t.Errorf("Error running gendoc command: %v", err)
    73  	}
    74  	gm.Verify(t, buf.Bytes())
    75  }