<commit_before>// Copyright 2015 The Go 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 vendorfile

import (
	"bytes"
	"strings"
	"testing"
)

func TestUpdate(t *testing.T) {
	var from = `{
	"Tool": "github.com/kardianos/govendor",
	"Package": [
		{
			"Vendor": "github.com/dchest/safefile",
			"Local": "github.com/kardianos/govendor/internal/github.com/dchest/safefile",
			"Version": "74b1ec0619e722c9f674d1a21e1a703fe90c4371",
			"VersionTime": "2015-04-10T19:48:00+02:00"
		}
	]
}`
	var to = `{
	"comment": "",
	"package": [
		{
			"canonical": "github.com/dchest/safefile",
			"comment": "",
			"local": "github.com/kardianos/govendor/internal/github.com/dchest/safefile",
			"revision": "74b1ec0619e722c9f674d1a21e1a703fe90<PASSWORD>",
			"revisionTime": "2015-04-10T19:48:00+02:00"
		}
	]
}`

	vf := &File{}

	err := vf.Unmarshal(strings.NewReader(from))
	if err != nil {
		t.Fatal(err)
	}

	buf := &bytes.Buffer{}
	err = vf.Marshal(buf)
	if err != nil {
		t.Fatal(err)
	}

	if buf.String() != to {
		t.Fatal("Got:", buf.String())
	}
}

func TestRemove(t *testing.T) {
	var from = `{
	"package": [
		{
			"canonical": "pkg1"
		},
		{
			"canonical": "pkg2"
		},
		{
			"canonical": "pkg3"
		}
	]
}`
	var to = `{
	"comment": "",
	"package": [
		{
			"canonical": "pkg1",
			"comment": "",
			"local": "",
			"revision": "",
			"revisionTime": ""
		}
	]
}`

	vf := &File{}

	err := vf.Unmarshal(strings.NewReader(from))
	if err != nil {
		t.Fatal(err)
	}

	vf.Package[1].Remove = true
	vf.Package[2].Remove = true

	buf := &bytes.Buffer{}
	err = vf.Marshal(buf)
	if err != nil {
		t.Fatal(err)
	}

	if buf.String() != to {
		t.Fatal("Got:", buf.String())
	}
}

func TestAdd(t *testing.T) {
	var from = `{
	"package": [
		{
			"canonical": "pkg1"
		}
	]
}`
	var to = `{
	"comment": "",
	"package": [
		{
			"canonical": "pkg1",
			"comment": "",
			"local": "",
			"revision": "",
			"revisionTime": ""
		},
		{
			"canonical": "pkg2",<commit_msg>vendorfile: update test to include ignore field.<commit_after>// Copyright 2015 The Go 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 vendorfile

import (
	"bytes"
	"strings"
	"testing"
)

func TestUpdate(t *testing.T) {
	var from = `{
	"Tool": "github.com/kardianos/govendor",
	"Package": [
		{
			"Vendor": "github.com/dchest/safefile",
			"Local": "github.com/kardianos/govendor/internal/github.com/dchest/safefile",
			"Version": "74b1ec0619e722c9f674d1a21e1a703fe90c4371",
			"VersionTime": "2015-04-10T19:48:00+02:00"
		}
	]
}`
	var to = `{
	"comment": "",
	"ignore": "",
	"package": [
		{
			"canonical": "github.com/dchest/safefile",
			"comment": "",
			"local": "github.com/kardianos/govendor/internal/github.com/dchest/safefile",
			"revision": "74b1ec0619e722c9f674d1a21e1a703fe90c4371",
			"revisionTime": "2015-04-10T19:48:00+02:00"
		}
	]
}`

	vf := &File{}

	err := vf.Unmarshal(strings.NewReader(from))
	if err != nil {
		t.Fatal(err)
	}

	buf := &bytes.Buffer{}
	err = vf.Marshal(buf)
	if err != nil {
		t.Fatal(err)
	}

	if buf.String() != to {
		t.Fatal("Got:", buf.String())
	}
}

func TestRemove(t *testing.T) {
	var from = `{
	"package": [
		{
			"canonical": "pkg1"
		},
		{
			"canonical": "pkg2"
		},
		{
			"canonical": "pkg3"
		}
	]
}`
	var to = `{
	"comment": "",
	"ignore": "",
	"package": [
		{
			"canonical": "pkg1",
			"comment": "",
			"local": "",
			"revision": "",
			"revisionTime": ""
		}
	]
}`

	vf := &File{}

	err := vf.Unmarshal(strings.NewReader(from))
	if err != nil {
		t.Fatal(err)
	}

	vf.Package[1].Remove = true
	vf.Package[2].Remove = true

	buf := &bytes.Buffer{}
	err = vf.Marshal(buf)
	if err != nil {
		t.Fatal(err)
	}

	if buf.String() != to {
		t.Fatal("Got:", buf.String())
	}
}

func TestAdd(t *testing.T) {
	var from = `{
	"package": [
		{
			"canonical": "pkg1"
		}
	]
}`
	var to = `{
	"comment": "",
	"ignore": "",
	"package": [
		{
			"canonical": "pkg1",
			"comment": "",
			"local": "",
			"revision": "",
			"revisionTime": ""
		},
		{
			"canonical": "pkg2",
{'fraction_non_alphanumeric': 0.21146008151522416, 'fraction_numerical': 0.04842963318149125, 'mean_word_length': 3.21840242669363, 'pattern_counts': {'":': 71, '<': 4, '<?xml version=': 0, '>': 4, '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': '737081', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1768, 'n_words': 310}