github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/mdtogo/common/common_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 common_test
    16  
    17  import (
    18  	"os"
    19  	"path"
    20  	"testing"
    21  
    22  	"github.com/GoogleContainerTools/kpt/mdtogo/common"
    23  	"github.com/stretchr/testify/assert"
    24  )
    25  
    26  func TestReadingMarkdownDirectoryRecursively(t *testing.T) {
    27  	parentTestDir := t.TempDir()
    28  	childTestDir, dirErr := os.MkdirTemp(parentTestDir, "test")
    29  	assert.NoError(t, dirErr)
    30  
    31  	firstTestFile, _ := os.Create(path.Join(parentTestDir, "example1.md"))
    32  	secondTestFile, _ := os.Create(path.Join(childTestDir, "example2.md"))
    33  	files, err := common.ReadFiles(parentTestDir, true)
    34  	assert.NoError(t, err)
    35  	assert.Equal(t, 2, len(files))
    36  	assert.Contains(t, files, firstTestFile.Name())
    37  	assert.Contains(t, files, secondTestFile.Name())
    38  }
    39  
    40  func TestReadingMarkdownDirectoryNonrecursively(t *testing.T) {
    41  	testDir := t.TempDir()
    42  	firstTestFile, _ := os.Create(path.Join(testDir, "example1.md"))
    43  	secondTestFile, _ := os.Create(path.Join(testDir, "example2.md"))
    44  	files, err := common.ReadFiles(testDir, false)
    45  	assert.NoError(t, err)
    46  	assert.Equal(t, 2, len(files))
    47  	assert.Contains(t, files, firstTestFile.Name())
    48  	assert.Contains(t, files, secondTestFile.Name())
    49  }
    50  
    51  func TestReadingMarkdownFileNonrecursively(t *testing.T) {
    52  	testDir := t.TempDir()
    53  	testFile, _ := os.Create(path.Join(testDir, "examples.md"))
    54  	files, err := common.ReadFiles(testFile.Name(), false)
    55  	assert.NoError(t, err)
    56  	assert.Equal(t, 1, len(files))
    57  	assert.Contains(t, files, testFile.Name())
    58  }
    59  
    60  func TestReadingMarkdownFileRecursively(t *testing.T) {
    61  	testDir := t.TempDir()
    62  	testFile, _ := os.Create(path.Join(testDir, "examples.md"))
    63  	files, err := common.ReadFiles(testFile.Name(), true)
    64  	assert.NoError(t, err)
    65  	assert.Equal(t, 1, len(files))
    66  	assert.Contains(t, files, testFile.Name())
    67  }