github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/internal/operator/nodeagent/dep/iteratefile_test.go (about)

     1  package dep_test
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/caos/orbos/internal/operator/nodeagent/dep"
     9  )
    10  
    11  func TestIterateFile(t *testing.T) {
    12  
    13  	from := `first line
    14  second line
    15  third line
    16  `
    17  
    18  	expect := `first line
    19  #third line
    20  fourth line
    21  `
    22  
    23  	reader := strings.NewReader(from)
    24  
    25  	writer := new(bytes.Buffer)
    26  	defer writer.Reset()
    27  
    28  	if err := dep.Manipulate(reader, writer, []string{"second"}, []string{"fourth line"}, func(line string) *string {
    29  		if strings.Contains(line, "third") {
    30  			str := "#" + line
    31  			return &str
    32  		}
    33  		return &line
    34  	}); err != nil {
    35  		t.Fatal(err)
    36  	}
    37  
    38  	result := writer.String()
    39  	if result != expect {
    40  		t.Errorf("Expected:\n%s\n\nGenerated:\n%s", expect, result)
    41  	}
    42  }