<commit_before>package main

import (
	"io/ioutil"
	"os"
	"os/exec"
	"testing"

	"github.com/dynport/dgtk/goassets/goassets"
	. "github.com/smartystreets/goconvey/convey"
)

func cleanup(t *testing.T) {
	os.Remove(modulePath)
	os.RemoveAll("./tmp")
}

var modulePath = "./fixtures/assets.go"

func TestAssets(t *testing.T) {
	assets := &goassets.Assets{Paths: []string{"./fixtures"}, CustomPackagePath: modulePath}
	Convey("Assets", t, func() {
		Convey("AssetsPaths", func() {
			So(assets, ShouldNotBeNil)
			paths, e := assets.AssetPaths()
			So(e, ShouldBeNil)
			So(len(paths), ShouldEqual, 2)
			So(paths[0].Path, ShouldEqual, "fixtures/a.html")
			So(paths[0].Key, ShouldEqual, "a.html")
			So(paths[1].Path, ShouldEqual, "fixtures/vendor/jquery.js")
			So(paths[1].Key, ShouldEqual, "vendor/jquery.js")
		})
		Convey("Build", func() {
			Convey("raise an error when file exists", func() {
				e := ioutil.WriteFile(modulePath, []byte("//just some comment"), 0644)
				if e != nil {
					t.Fatal(e.Error())
				}
				e = assets.Build()
				So(e, ShouldNotBeNil)
				So(e.Error(), ShouldContainSubstring, "already exists")
			})

			Convey("with assets.go not exising", func() {
				cleanup(t)
				e := assets.Build()
				So(e, ShouldBeNil)
			})
		})
	})
}

func fileExists(p string) bool {
	_, e := os.Stat(p)
	if e != nil {
		return false
	}
	return true
}

func TestIntegration(t *testing.T) {
	Convey("Integration test", t, func() {
		cleanup(t)
		os.MkdirAll("./tmp", 0755)
		e := ioutil.WriteFile("./tmp/main.go", []byte(TEST_FILE), 0755)
		if e != nil {
			t.Fatal(e.Error())
		}
		So(fileExists("./tmp/assets.go"), ShouldBeFalse)
		cmd := exec.Command("bash", "-c", "cd tmp && goassets ../fixtures && go run *.go")
		b, e := cmd.CombinedOutput()
		So(e, ShouldBeNil)
		out := string(b)
		So(fileExists("./tmp/assets.go"), ShouldBeTrue)
		So(out, ShouldContainSubstring, "a.html: 21")
		So(out, ShouldContainSubstring, "vendor/jquery.js: 15")
	})
}

const TEST_FILE = `
package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Println("running")
	for _, name := range assetNames() {
		b, e := readAsset(name)
		if e != nil {
			fmt.Println("ERROR: " + e.Error())
			os.Exit(1)<commit_msg>rename to make lint happy<commit_after>package main

import (
	"io/ioutil"
	"os"
	"os/exec"
	"testing"

	"github.com/dynport/dgtk/goassets/goassets"
	. "github.com/smartystreets/goconvey/convey"
)

func cleanup(t *testing.T) {
	os.Remove(modulePath)
	os.RemoveAll("./tmp")
}

var modulePath = "./fixtures/assets.go"

func TestAssets(t *testing.T) {
	assets := &goassets.Assets{Paths: []string{"./fixtures"}, CustomPackagePath: modulePath}
	Convey("Assets", t, func() {
		Convey("AssetsPaths", func() {
			So(assets, ShouldNotBeNil)
			paths, e := assets.AssetPaths()
			So(e, ShouldBeNil)
			So(len(paths), ShouldEqual, 2)
			So(paths[0].Path, ShouldEqual, "fixtures/a.html")
			So(paths[0].Key, ShouldEqual, "a.html")
			So(paths[1].Path, ShouldEqual, "fixtures/vendor/jquery.js")
			So(paths[1].Key, ShouldEqual, "vendor/jquery.js")
		})
		Convey("Build", func() {
			Convey("raise an error when file exists", func() {
				e := ioutil.WriteFile(modulePath, []byte("//just some comment"), 0644)
				if e != nil {
					t.Fatal(e.Error())
				}
				e = assets.Build()
				So(e, ShouldNotBeNil)
				So(e.Error(), ShouldContainSubstring, "already exists")
			})

			Convey("with assets.go not exising", func() {
				cleanup(t)
				e := assets.Build()
				So(e, ShouldBeNil)
			})
		})
	})
}

func fileExists(p string) bool {
	_, e := os.Stat(p)
	if e != nil {
		return false
	}
	return true
}

func TestIntegration(t *testing.T) {
	Convey("Integration test", t, func() {
		cleanup(t)
		os.MkdirAll("./tmp", 0755)
		e := ioutil.WriteFile("./tmp/main.go", []byte(testFile), 0755)
		if e != nil {
			t.Fatal(e.Error())
		}
		So(fileExists("./tmp/assets.go"), ShouldBeFalse)
		cmd := exec.Command("bash", "-c", "cd tmp && goassets ../fixtures && go run *.go")
		b, e := cmd.CombinedOutput()
		So(e, ShouldBeNil)
		out := string(b)
		So(fileExists("./tmp/assets.go"), ShouldBeTrue)
		So(out, ShouldContainSubstring, "a.html: 21")
		So(out, ShouldContainSubstring, "vendor/jquery.js: 15")
	})
}

const testFile = `
package main

import (
	"fmt"
	"os"
)

func main() {
	fmt.Println("running")
	for _, name := range assetNames() {
		b, e := readAsset(name)
		if e != nil {
			fmt.Println("ERROR: " + e.Error())
			os.Exit(1)
{'fraction_non_alphanumeric': 0.19021739130434784, 'fraction_numerical': 0.009963768115942028, 'mean_word_length': 3.6839872746553555, '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': '1271449', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1719, 'n_words': 347}