Exemple #1
0
func TestNewContent(t *testing.T) {
	initViper()

	err := initFs()
	if err != nil {
		t.Fatalf("initialization error: %s", err)
	}

	cases := []struct {
		kind          string
		path          string
		resultStrings []string
	}{
		{"post", "post/sample-1.md", []string{`title = "sample 1"`, `test = "test1"`}},
		{"stump", "stump/sample-2.md", []string{`title = "sample 2"`}},     // no archetype file
		{"", "sample-3.md", []string{`title = "sample 3"`}},                // no archetype
		{"product", "product/sample-4.md", []string{`title = "sample 4"`}}, // empty archetype front matter
	}

	for i, c := range cases {
		err = create.NewContent(hugofs.Source(), c.kind, c.path)
		if err != nil {
			t.Errorf("[%d] NewContent: %s", i, err)
		}

		fname := filepath.Join(os.TempDir(), "content", filepath.FromSlash(c.path))
		_, err = hugofs.Source().Stat(fname)
		if err != nil {
			t.Errorf("[%d] Stat: %s", i, err)
		}

		for _, v := range c.resultStrings {
			found, err := afero.FileContainsBytes(hugofs.Source(), fname, []byte(v))
			if err != nil {
				t.Errorf("[%d] FileContainsBytes: %s", i, err)
			}
			if !found {
				t.Errorf("content missing from output: %q", v)
			}
		}
	}
}
Exemple #2
0
// FileContains checks if a file contains a specified string.
func FileContains(filename string, subslice []byte, fs afero.Fs) (bool, error) {
	return afero.FileContainsBytes(fs, filename, subslice)
}