Increasing coverage in command package

Hi folks.

I updated my pr with some fixes and added a couple of new tests. Hugo.go’s coverage is now 40.1% up from 0.:slight_smile:

https://github.com/spf13/hugo/pull/1834 enjoy.

1 Like

So what should I do, to get it merged folks? Or is there anything you want me to still do with it?

You have to wait for me to get around to look at it, I guess. I have a limited amount of time to spend on Hugo, and some times I choose to spend it on writing code myself.

1 Like

Thanks @bep. That’s cool, I’ll wait, I was just wondering if you need anything from me! :slightly_smiling:

Guys. I need assistance.

Turns out, using MemFs intermittently fails to create the files and directories it needs.

For example:

Congratulations! Your new Hugo site is created in /var/folders/yd/7ghbhjz521d5lk76j265_ymw0000gp/T/blog20.

Just a few more steps and you're ready to go:

1. Download a theme into the same-named folder.
   Choose a theme from https://themes.gohugo.io/, or
   create your own with the "hugo new theme <THEMENAME>" command.
2. Perhaps you want to add some content. You can add single files
   with "hugo new <SECTIONNAME>/<FILENAME>.<FORMAT>".
3. Start the built-in live server via "hugo server".

Visit https://gohugo.io/ for quickstart guide and full documentation.
Error: Unable to locate Config file. Perhaps you need to create a new site.
       Run `hugo help new` for details. (open /var/folders/yd/7ghbhjz521d5lk76j265_ymw0000gp/T/blog20/config.toml: no such file or directory)

This shows that the directory and site was created successfully, there was no error, yet, the config toml file cannot be found.

The code:

	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	blogSuffix := strconv.Itoa(r.Intn(math.MaxInt8))
	basepath := filepath.Join(os.TempDir(), "blog"+blogSuffix)
	configPath := filepath.Join(basepath, "config.toml")
	hugofs.InitMemFs()
	hugofs.SetSource(afero.NewMemMapFs())
	hugofs.SetDestination(afero.NewMemMapFs())
	err := doNewSite(basepath, true)
	assert.Nil(t, err)
	basicConfig := `
		source = ` + strconv.Quote(basepath) + `
        contentdir = "content" 
        layoutdir = "layout"
		publishdir = "public"
        baseurl = "http://localhost"
        canonifyurls = true`
	source = basepath
	baseURL = "htt://localhost"
	hugofs.Source().Create(configPath)
	hugofs.Source().Chmod(configPath, os.ModeTemporary|os.ModePerm)
	afero.WriteFile(hugofs.Source(), configPath, []byte(basicConfig), os.ModeTemporary|os.ModePerm)
	cfgFile = configPath
	// Workaround for go test -coverprofile=cover.out since viper parses command-line
	// arguments.
	if len(os.Args) > 1 {
		os.Args = os.Args[:1]
	}
	Execute()

And the problem is, that most of the time, this passes nicely. But some of the time, it fails, or the code is looking too fast, I don’t know. :confused: Any pointers as to why this is happening?

Thanks!

/var/folders/yd/7ghbhjz521d5lk76j265_ymw0000gp/T

Why do you store your content in (what looks like a) TEMP dir when you have a virtual filesystem all to yourself?

Oh, I see. So I’m using MemFs incorrectly? I still need to set things like, where the config file is located for Initconfiguration and things like that. And site create. So how you do that with MemFs?

For example, when looking for usage, I could find this example:

		dir = filepath.Join(os.TempDir(), dir)

In content_test.go. It’s using memfs, but providing a directory which is created through hugofs.Source().Create(). Which is setup as MemFs. So, if I understand this correctly, this is how it’s supposed to be used.

There are no reference to TempDir in content_test.go, there are, however, plenty of examples of using the memfs in hugosites_test.go. If you still cannot get it to work, I don’t think I’m able to help you.

Also, there is no magic about the Afero filesystems, they work similar to the real one, i.e. you need to create a folder before you dump files into it etc.

What do you mean there are no references? I’d call this a pretty solid reference.

fname := filepath.Join(os.TempDir(), "content", filepath.FromSlash(c.path))
		_, err = hugofs.Source().Stat(fname)

Thanks for the reference. I think I saw that file, with all the config files coded in functions and variables. Interesting approach. Though I like the temp folders better, since they are actually testing file access and behavior. Whilst the memory stuff might not catch things like, overlapping access.

I’ll leave like 1-2 tests in there for that. The template stuff must have a folder anyways.

OK, we have 2 content_test.go file – but that wasn’1t my main point, so not very useful to be pedantic about it.

I’m not pedantic dude. :slight_smile: You were the one replying with the usual arrogance of yours. But that’s fine. I’m used to it by now.

I’d recommend always using MemMapFs during tests. It just keeps things cleaner, and we don’t have to worry about cleaning up all of the tmp files.

Push what you have to your PR branch. I should be able to take a quick look at it this morning.

1 Like

Thanks! I’m gonna update it with properly using MemFs. :slightly_smiling:

Well, you cannot blame me for being impatient, having followed up on your PR for the last 8 months.

The above was satire, which could be mistaken for arrogance, I guess.

Please keep further discussions in one place, at GitHub.