<commit_before>// Copyright 2016 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import (
	"fmt"
	"path/filepath"
	"strings"
	"testing"

	"time"

	"github.com/spf13/afero"

	"github.com/spf13/hugo/deps"
	"github.com/spf13/hugo/hugofs"
	"github.com/stretchr/testify/require"
)

/*
	This file will test the "making everything a page" transition.

	See https://github.com/spf13/hugo/issues/2297

*/

func TestNodesAsPage(t *testing.T) {
	t.Parallel()
	for _, preserveTaxonomyNames := range []bool{false, true} {
		for _, ugly := range []bool{true, false} {
			doTestNodeAsPage(t, ugly, preserveTaxonomyNames)
		}
	}
}

func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) {

	/* Will have to decide what to name the node content files, but:

		Home page should have:
		Content, shortcode support
	   	Metadata (title, dates etc.)
		Params
	   	Taxonomies (categories, tags)

	*/

	var (
		cfg, fs = newTestCfg()
		th      = testHelper{cfg, fs, t}
	)

	cfg.Set("uglyURLs", ugly)
	cfg.Set("preserveTaxonomyNames", preserveTaxonomyNames)

	cfg.Set("paginate", 1)
	cfg.Set("title", "Hugo Rocks")
	cfg.Set("rssURI", "customrss.xml")

	writeLayoutsForNodeAsPageTests(t, fs)
	writeNodePagesForNodeAsPageTests(t, fs, "")

	writeRegularPagesForNodeAsPageTests(t, fs)

	sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})

	require.NoError(t, err)

	require.NoError(t, sites.Build(BuildCfg{}))

	// date order: home, sect1, sect2, cat/hugo, cat/web, categories

	th.assertFileContent(filepath.Join("public", "index.html"),
		"Index Title: Home Sweet Home!",
		"Home <strong>Content!</strong>",
		"# Pages: 4",
		"Date: 2009-01-02",
		"Lastmod: 2009-01-03",
		"GetPage: Section1 ",
	)

	th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "regular1"), "Single Title: Page 01", "Content Page 01")

	nodes := sites.findAllPagesByKindNotIn(KindPage)

	require.Len(t, nodes, 7)

	home := nodes[6] // oldest<commit_msg>hugolib: Fix broken test<commit_after>// Copyright 2016 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import (
	"fmt"
	"path/filepath"
	"strings"
	"testing"

	"time"

	"github.com/spf13/afero"

	"github.com/spf13/hugo/deps"
	"github.com/spf13/hugo/hugofs"
	"github.com/stretchr/testify/require"
)

/*
	This file will test the "making everything a page" transition.

	See https://github.com/spf13/hugo/issues/2297

*/

func TestNodesAsPage(t *testing.T) {
	t.Parallel()
	for _, preserveTaxonomyNames := range []bool{false, true} {
		for _, ugly := range []bool{true, false} {
			doTestNodeAsPage(t, ugly, preserveTaxonomyNames)
		}
	}
}

func doTestNodeAsPage(t *testing.T, ugly, preserveTaxonomyNames bool) {

	/* Will have to decide what to name the node content files, but:

		Home page should have:
		Content, shortcode support
	   	Metadata (title, dates etc.)
		Params
	   	Taxonomies (categories, tags)

	*/

	var (
		cfg, fs = newTestCfg()
		th      = testHelper{cfg, fs, t}
	)

	cfg.Set("uglyURLs", ugly)
	cfg.Set("preserveTaxonomyNames", preserveTaxonomyNames)

	cfg.Set("paginate", 1)
	cfg.Set("title", "Hugo Rocks")
	cfg.Set("rssURI", "customrss.xml")

	writeLayoutsForNodeAsPageTests(t, fs)
	writeNodePagesForNodeAsPageTests(t, fs, "")

	writeRegularPagesForNodeAsPageTests(t, fs)

	sites, err := NewHugoSites(deps.DepsCfg{Fs: fs, Cfg: cfg})

	require.NoError(t, err)

	require.NoError(t, sites.Build(BuildCfg{}))

	// date order: home, sect1, sect2, cat/hugo, cat/web, categories

	th.assertFileContent(filepath.Join("public", "index.html"),
		"Index Title: Home Sweet Home!",
		"Home <strong>Content!</strong>",
		"# Pages: 4",
		"Date: 2009-01-02",
		"Lastmod: 2009-01-03",
		"GetPage: Section1 ",
	)

	th.assertFileContent(expectedFilePath(ugly, "public", "sect1", "regular1"), "Single Title: Page 01", "Content Page 01")

	nodes := sites.findAllPagesByKindNotIn(KindPage)

	require.Len(t, nodes, 8)

	home := nodes[6] // oldest
{'fraction_non_alphanumeric': 0.13457308538292342, 'fraction_numerical': 0.019596080783843232, 'mean_word_length': 4.1460905349794235, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2333855', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1701, 'n_words': 482}