<commit_before>// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package main

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

	"github.com/spf13/afero"

	"code.google.com/p/go.exp/inotify"
)

func TestTail(t *testing.T) {
	d, err := ioutil.TempDir("", "tail_test")
	if err != nil {
		t.Error(err)
	}
	defer os.RemoveAll(d)

	logfile := "/tmp/log"
	f, err := os.Create(logfile)
	if err != nil {
		t.Error(err)
	}
	defer f.Close()

	w, err := NewFakeWatcher()
	if err != nil {
		t.Fatalf("Couldn't make a watcher: %s", err)
	}
	lines := make(chan string)
	ta := NewTailer(lines, w, &afero.OsFs{})
	if ta == nil {
		t.Fatalf("Couldn't make a tailer.")
	}
	defer ta.Stop()
	ta.Tail(logfile)

	if !w.IsWatching(logfile) {
		t.Errorf("Not watching logfile %s: only %+#v\n", logfile, w.watches)
	}

	w.InjectEvent(&inotify.Event{Mask: inotify.IN_CREATE | inotify.IN_ONLYDIR,
		Name: logfile})

	if _, ok := ta.files[logfile]; !ok {
		t.Errorf("path not found in files map: %+#v", ta.files)
	}
}

func TestHandleLogChange(t *testing.T) {
	d, err := ioutil.TempDir("", "tail_test")
	if err != nil {
		t.Error(err)
	}
	defer os.RemoveAll(d)

	logfile := d + "/log"
	f, err := os.Create(logfile)
	if err != nil {
		t.Error(err)
	}
	defer f.Close()

	w, err := NewInotifyWatcher()
	if err != nil {
		t.Fatalf("Couldn't make a watcher: %s", err)
	}
	lines := make(chan string)
	ta := NewTailer(lines, w, &afero.OsFs{})
	if ta == nil {
		t.Fatalf("Couldn't make a tailer.")
	}
	defer ta.Stop()
	ta.Tail(logfile)

	_, err = f.WriteString("a\nb\nc\nd\n")
	if err != nil {
		t.Error(err)
	}
	go ta.handleLogUpdate(logfile)

	for _, expected := range []string{"a", "b", "c", "d"} {
		// Run as a goroutine because it's going to emit lines via output channel
		line := <-ta.lines
		if line != expected {
			t.Errorf("line doesn't match:\n\texpected: %s\n\tgot: %s", expected, line)
			continue
		}
	}
}

func TestHandleLogChangePartialLine(t *testing.T) {
	d, err := ioutil.TempDir("", "tail_test")
	if err != nil {
		t.Error(err)
	}
	defer os.RemoveAll(d)

	logfile := d + "/log"
	f, err := os.Create(logfile)
	if err != nil {<commit_msg>Convert the test to use the in memory filesystem.<commit_after>// Copyright 2011 Google Inc. All Rights Reserved.
// This file is available under the Apache license.

package main

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

	"github.com/spf13/afero"

	"code.google.com/p/go.exp/inotify"
)

func TestTail(t *testing.T) {
	fs := &afero.MemMapFs{}
	fs.Mkdir("tail_test", os.ModePerm)
	logfile := "/tmp/log"
	f, err := fs.Create(logfile)
	if err != nil {
		t.Error(err)
	}
	defer f.Close()

	w, err := NewFakeWatcher()
	if err != nil {
		t.Fatalf("Couldn't make a watcher: %s", err)
	}
	lines := make(chan string)
	ta := NewTailer(lines, w, fs)
	if ta == nil {
		t.Fatalf("Couldn't make a tailer.")
	}
	defer ta.Stop()
	ta.Tail(logfile)

	if !w.IsWatching(logfile) {
		t.Errorf("Not watching logfile %s: only %+#v\n", logfile, w.watches)
	}

	w.InjectEvent(&inotify.Event{Mask: inotify.IN_CREATE | inotify.IN_ONLYDIR,
		Name: logfile})

	if _, ok := ta.files[logfile]; !ok {
		t.Errorf("path not found in files map: %+#v", ta.files)
	}
}

func TestHandleLogChange(t *testing.T) {
	fs := &afero.MemMapFs{}
	err := fs.Mkdir("/tail_test", os.ModePerm)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	logfile := "/tail_test/log"
	f, err := fs.Create(logfile)
	if err != nil {
		t.Fatalf("err: %s", err)
	}
	defer f.Close()

	w, err := NewFakeWatcher()
	if err != nil {
		t.Fatalf("Couldn't make a watcher: %s", err)
	}
	lines := make(chan string, 4)
	ta := NewTailer(lines, w, fs)
	if ta == nil {
		t.Fatalf("Couldn't make a tailer.")
	}
	defer ta.Stop()
	t.Logf("hi")
	ta.Tail(logfile)

	_, err = f.WriteString("a\nb\nc\nd\n")
	if err != nil {
		t.Error(err)
	}
	t.Logf("hi")
	f.Seek(0, os.SEEK_SET)
	f.Close()
	go ta.handleLogUpdate(logfile)
	// var result string[]
	// for _, line := range(ta.lines) {
	// 	result = append(result, line)
	// }

	t.Logf("hi again")

	for _, expected := range []string{"a", "b", "c", "d"} {
		t.Logf("hi")
		// Run as a goroutine because it's going to emit lines via output channel
		line := <-ta.lines
		if line != expected {
			t.Errorf("line doesn't match:\n\texpected: %s\n\tgot: %s", expected, line)
			continue
		}
	}
}

func TestHandleLogChangePartialLine(t *testing.T) {
	d, err := ioutil.TempDir("", "tail_test")
	if err != nil {
		t.Error(err)
	}
	defer os.RemoveAll(d)

	logfile := d + "/log"
	f, err := os.Create(logfile)
	if err != nil {
{'fraction_non_alphanumeric': 0.18370044052863435, 'fraction_numerical': 0.0030837004405286344, 'mean_word_length': 3.3916827852998064, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5779342', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1913, 'n_words': 464}