hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

testdata_builder.go (1636B)

    1 // Copyright 2018 The Hugo Authors. All rights reserved.
    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 // http://www.apache.org/licenses/LICENSE-2.0
    7 //
    8 // Unless required by applicable law or agreed to in writing, software
    9 // distributed under the License is distributed on an "AS IS" BASIS,
   10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   11 // See the License for the specific language governing permissions and
   12 // limitations under the License.
   13 
   14 package htesting
   15 
   16 import (
   17 	"path/filepath"
   18 	"testing"
   19 
   20 	"github.com/spf13/afero"
   21 )
   22 
   23 type testFile struct {
   24 	name    string
   25 	content string
   26 }
   27 
   28 type testdataBuilder struct {
   29 	t          testing.TB
   30 	fs         afero.Fs
   31 	workingDir string
   32 
   33 	files []testFile
   34 }
   35 
   36 func NewTestdataBuilder(fs afero.Fs, workingDir string, t testing.TB) *testdataBuilder {
   37 	workingDir = filepath.Clean(workingDir)
   38 	return &testdataBuilder{fs: fs, workingDir: workingDir, t: t}
   39 }
   40 
   41 func (b *testdataBuilder) Add(filename, content string) *testdataBuilder {
   42 	b.files = append(b.files, testFile{name: filename, content: content})
   43 	return b
   44 }
   45 
   46 func (b *testdataBuilder) Build() *testdataBuilder {
   47 	for _, f := range b.files {
   48 		if err := afero.WriteFile(b.fs, filepath.Join(b.workingDir, f.name), []byte(f.content), 0666); err != nil {
   49 			b.t.Fatalf("failed to add %q: %s", f.name, err)
   50 		}
   51 	}
   52 	return b
   53 }
   54 
   55 func (b testdataBuilder) WithWorkingDir(dir string) *testdataBuilder {
   56 	b.workingDir = filepath.Clean(dir)
   57 	b.files = make([]testFile, 0)
   58 	return &b
   59 }