<commit_before>// Copyright 2016-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"testing"

	"github.com/u-root/u-root/pkg/testutil"
)

type test struct {
	args    []string
	expects string
}

func run(c *exec.Cmd) (string, string, error) {
	var o, e bytes.Buffer
	c.Stdout, c.Stderr = &o, &e
	err := c.Run()
	return o.String(), e.String(), err
}

func TestMknodFifo(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "ls")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	// Delete a preexisting pipe if it exists, thought it shouldn't
	pipepath := filepath.Join(tmpDir, "testpipe")
	_ = os.Remove(pipepath)
	if _, err := os.Stat(pipepath); err != nil && !os.IsNotExist(err) {
		// Couldn't delete the file for reasons other than it didn't exist.
		t.Fatalf("couldn't delete preexisting pipe")
	}

	// Make a pipe and check that it exists.
	fmt.Print(pipepath)
	c := testutil.Command(t, pipepath, "p")
	c.Run()
	if _, err := os.Stat(pipepath); os.IsNotExist(err) {
		t.Errorf("Pipe was not created.")
	}
}

func TestInvocationErrors(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "ls")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	devpath := filepath.Join(tmpDir, "testdev")
	var tests = []test{
		{args: []string{devpath}, expects: "Usage: mknod path type [major minor]\n"},
		{args: []string{""}, expects: "Usage: mknod path type [major minor]\n"},
		{args: []string{devpath, "p", "254", "3"}, expects: "device type p requires no other arguments\n"},
		{args: []string{devpath, "b", "254"}, expects: "Usage: mknod path type [major minor]\n"},
		{args: []string{devpath, "b"}, expects: "device type b requires a major and minor number\n"},
		{args: []string{devpath, "k"}, expects: "device type not recognized: k\n"},
	}

	for _, v := range tests {
		c := testutil.Command(t, v.args...)
		_, e, _ := run(c)
		if e[20:] != v.expects {
			t.Errorf("mknod for '%v' failed: got '%s', want '%s'", v.args, e[20:], v.expects)
		}
	}
}

func TestMain(m *testing.M) {
	testutil.Run(m, main)
}
<commit_msg>mknod_test: don't print spam<commit_after>// Copyright 2016-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"testing"

	"github.com/u-root/u-root/pkg/testutil"
)

type test struct {
	args    []string
	expects string
}

func run(c *exec.Cmd) (string, string, error) {
	var o, e bytes.Buffer
	c.Stdout, c.Stderr = &o, &e
	err := c.Run()
	return o.String(), e.String(), err
}

func TestMknodFifo(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "ls")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	// Delete a preexisting pipe if it exists, thought it shouldn't
	pipepath := filepath.Join(tmpDir, "testpipe")
	_ = os.Remove(pipepath)
	if _, err := os.Stat(pipepath); err != nil && !os.IsNotExist(err) {
		// Couldn't delete the file for reasons other than it didn't exist.
		t.Fatalf("couldn't delete preexisting pipe")
	}

	// Make a pipe and check that it exists.
	c := testutil.Command(t, pipepath, "p")
	c.Run()
	if _, err := os.Stat(pipepath); os.IsNotExist(err) {
		t.Errorf("Pipe was not created.")
	}
}

func TestInvocationErrors(t *testing.T) {
	tmpDir, err := ioutil.TempDir("", "ls")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpDir)

	devpath := filepath.Join(tmpDir, "testdev")
	var tests = []test{
		{args: []string{devpath}, expects: "Usage: mknod path type [major minor]\n"},
		{args: []string{""}, expects: "Usage: mknod path type [major minor]\n"},
		{args: []string{devpath, "p", "254", "3"}, expects: "device type p requires no other arguments\n"},
		{args: []string{devpath, "b", "254"}, expects: "Usage: mknod path type [major minor]\n"},
		{args: []string{devpath, "b"}, expects: "device type b requires a major and minor number\n"},
		{args: []string{devpath, "k"}, expects: "device type not recognized: k\n"},
	}

	for _, v := range tests {
		c := testutil.Command(t, v.args...)
		_, e, _ := run(c)
		if e[20:] != v.expects {
			t.Errorf("mknod for '%v' failed: got '%s', want '%s'", v.args, e[20:], v.expects)
		}
	}
}

func TestMain(m *testing.M) {
	testutil.Run(m, main)
}

{'fraction_non_alphanumeric': 0.17909439062851995, 'fraction_numerical': 0.008560486596080198, 'mean_word_length': 3.8, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3718394', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1691, 'n_words': 495}