github.com/wtsi-ssg/wrstat/v3@v3.2.3/combine/log_test.go (about)

     1  /*******************************************************************************
     2   * Copyright (c) 2022 Genome Research Ltd.
     3   *
     4   * Author: Sendu Bala <sb10@sanger.ac.uk>
     5   * 		   Kyle Mace  <km34@sanger.ac.uk>
     6   *
     7   * Permission is hereby granted, free of charge, to any person obtaining
     8   * a copy of this software and associated documentation files (the
     9   * "Software"), to deal in the Software without restriction, including
    10   * without limitation the rights to use, copy, modify, merge, publish,
    11   * distribute, sublicense, and/or sell copies of the Software, and to
    12   * permit persons to whom the Software is furnished to do so, subject to
    13   * the following conditions:
    14   *
    15   * The above copyright notice and this permission notice shall be included
    16   * in all copies or substantial portions of the Software.
    17   *
    18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    19   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    20   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    21   * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    22   * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    23   * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    24   * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    25   ******************************************************************************/
    26  
    27  package combine
    28  
    29  import (
    30  	"fmt"
    31  	"os"
    32  	"path/filepath"
    33  	"testing"
    34  
    35  	. "github.com/smartystreets/goconvey/convey"
    36  	"github.com/wtsi-ssg/wrstat/v3/fs"
    37  )
    38  
    39  // TestLogFiles tests that the log files merge and compress properly.
    40  func TestLogFiles(t *testing.T) {
    41  	Convey("Given log files and an output", t, func() {
    42  		inputs, output, outputPath := buildLogFiles(t)
    43  
    44  		Convey("you can merge and compress the log files to the output", func() {
    45  			err := LogFiles(inputs, output)
    46  			So(err, ShouldBeNil)
    47  
    48  			_, err = os.Stat(outputPath)
    49  			So(err, ShouldBeNil)
    50  
    51  			Convey("and the proper content exists within the output file", func() {
    52  				actualContent, err := fs.ReadCompressedFile(outputPath)
    53  				So(err, ShouldBeNil)
    54  
    55  				expectedContent := "This is line number0\nThis is line number1\nThis is line number2\n" +
    56  					"This is line number3\nThis is line number4\nThis is line number5\n"
    57  				So(actualContent, ShouldEqual, expectedContent)
    58  			})
    59  		})
    60  	})
    61  }
    62  
    63  // buildLogFiles builds six testing files, whereby each file contains a line
    64  // that reads, 'This is line number n', where n is the index of the for loop.
    65  func buildLogFiles(t *testing.T) ([]*os.File, *os.File, string) {
    66  	t.Helper()
    67  
    68  	paths := [6]string{"walk.1.log", "walk.2.log", "walk.3.log",
    69  		"walk.4.log", "walk.5.log", "walk.6.log"}
    70  	dir := t.TempDir()
    71  
    72  	inputs := make([]*os.File, len(paths))
    73  
    74  	for i, path := range paths {
    75  		f, err := os.Create(filepath.Join(dir, path))
    76  		if err != nil {
    77  			t.Fatal(err)
    78  		}
    79  
    80  		_, err = f.WriteString(fmt.Sprintf("This is line number%d\n", i))
    81  		if err != nil {
    82  			t.Fatal(err)
    83  		}
    84  
    85  		inputs[i] = f
    86  
    87  		f.Close()
    88  	}
    89  
    90  	filenames, err := fs.FindFilePathsInDir(dir, "")
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  
    95  	inputs, err = fs.OpenFiles(filenames)
    96  	if err != nil {
    97  		t.Fatal(err)
    98  	}
    99  
   100  	outputPath := filepath.Join(dir, "combine.log.gz")
   101  
   102  	fileOutput, err := os.Create(outputPath)
   103  	if err != nil {
   104  		t.Fatalf("create error: %s", err)
   105  	}
   106  
   107  	return inputs, fileOutput, outputPath
   108  }