summaryrefslogtreecommitdiff
path: root/integrationtests/dcat_line_endings_test.go
blob: 5484929360d7379a8bf3a3d9bf29c9abcab2b756 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package integrationtests

import (
	"bytes"
	"os"
	"os/exec"
	"testing"

	"github.com/mimecast/dtail/internal/config"
)

// TestDCatLineEndings verifies that dcat preserves line endings correctly
func TestDCatLineEndings(t *testing.T) {
	if !config.Env("DTAIL_INTEGRATION_TEST_RUN_MODE") {
		t.Log("Skipping")
		return
	}

	// Create a test file with various line endings
	testContent := "Line 1\nLine 2\nLine 3 with no ending"
	testFile := "test_line_endings.txt"
	
	if err := os.WriteFile(testFile, []byte(testContent), 0644); err != nil {
		t.Fatalf("Failed to create test file: %v", err)
	}
	defer os.Remove(testFile)

	t.Run("Serverless", func(t *testing.T) {
		// Run dcat in serverless mode
		cmd := exec.Command("../dcat", "--cfg", "none", testFile)
		output, err := cmd.Output()
		if err != nil {
			t.Fatalf("dcat command failed: %v", err)
		}

		// Expected output should have line endings preserved
		// Note: dcat adds a newline after the last line too
		expected := "Line 1\nLine 2\nLine 3 with no ending\n"
		
		if string(output) != expected {
			t.Errorf("Line endings not preserved correctly.\nExpected:\n%q\nGot:\n%q", 
				expected, string(output))
		}
	})

	t.Run("PlainMode", func(t *testing.T) {
		// Test with --plain flag which should preserve exact file content
		cmd := exec.Command("../dcat", "--plain", "--cfg", "none", testFile)
		output, err := cmd.Output()
		if err != nil {
			t.Fatalf("dcat command failed: %v", err)
		}

		// In plain mode, content should match exactly
		if string(output) != testContent {
			t.Errorf("Plain mode should preserve exact content.\nExpected:\n%q\nGot:\n%q", 
				testContent, string(output))
		}
	})

	t.Run("MultipleFiles", func(t *testing.T) {
		// Create additional test files
		testFile2 := "test_line_endings2.txt"
		testFile3 := "test_line_endings3.txt"
		
		if err := os.WriteFile(testFile2, []byte("File 2 Line 1\nFile 2 Line 2\n"), 0644); err != nil {
			t.Fatalf("Failed to create test file 2: %v", err)
		}
		defer os.Remove(testFile2)
		
		if err := os.WriteFile(testFile3, []byte("File 3 Line 1\n"), 0644); err != nil {
			t.Fatalf("Failed to create test file 3: %v", err)
		}
		defer os.Remove(testFile3)

		// Run dcat with multiple files
		cmd := exec.Command("../dcat", "--cfg", "none", testFile, testFile2, testFile3)
		output, err := cmd.Output()
		if err != nil {
			t.Fatalf("dcat command failed: %v", err)
		}

		// Verify that lines from all files are separated properly
		lines := bytes.Split(output, []byte{'\n'})
		// We expect 7 lines total (3 + 2 + 1 + empty line at end)
		if len(lines) != 7 {
			t.Errorf("Expected 7 lines, got %d. Output:\n%s", len(lines), string(output))
		}
	})

	t.Run("EmptyFile", func(t *testing.T) {
		// Test with empty file
		emptyFile := "empty.txt"
		if err := os.WriteFile(emptyFile, []byte(""), 0644); err != nil {
			t.Fatalf("Failed to create empty file: %v", err)
		}
		defer os.Remove(emptyFile)

		cmd := exec.Command("../dcat", "--cfg", "none", emptyFile)
		output, err := cmd.Output()
		if err != nil {
			t.Fatalf("dcat command failed: %v", err)
		}

		// Empty file should produce empty output
		if len(output) != 0 {
			t.Errorf("Expected empty output for empty file, got: %q", string(output))
		}
	})

	t.Run("CRLFLineEndings", func(t *testing.T) {
		// Test with Windows-style CRLF line endings
		crlfFile := "crlf_test.txt"
		crlfContent := "Line 1\r\nLine 2\r\nLine 3\r\n"
		
		if err := os.WriteFile(crlfFile, []byte(crlfContent), 0644); err != nil {
			t.Fatalf("Failed to create CRLF test file: %v", err)
		}
		defer os.Remove(crlfFile)

		// Run dcat in regular mode
		cmd := exec.Command("../dcat", "--cfg", "none", crlfFile)
		output, err := cmd.Output()
		if err != nil {
			t.Fatalf("dcat command failed: %v", err)
		}

		// In regular mode, CRLF should be normalized to LF
		expected := "Line 1\nLine 2\nLine 3\n"
		if string(output) != expected {
			t.Errorf("CRLF not handled correctly.\nExpected:\n%q\nGot:\n%q", 
				expected, string(output))
		}

		// In plain mode, CRLF should be preserved
		cmd = exec.Command("../dcat", "--plain", "--cfg", "none", crlfFile)
		output, err = cmd.Output()
		if err != nil {
			t.Fatalf("dcat --plain command failed: %v", err)
		}

		if string(output) != crlfContent {
			t.Errorf("Plain mode should preserve CRLF.\nExpected:\n%q\nGot:\n%q", 
				crlfContent, string(output))
		}
	})
}