extract_test.go raw

   1  package extract
   2  
   3  import (
   4  	"io"
   5  	"os"
   6  	"path/filepath"
   7  	"testing"
   8  )
   9  
  10  func TestPlainTextExtract(t *testing.T) {
  11  	dir := t.TempDir()
  12  	path := filepath.Join(dir, "test.txt")
  13  	content := "Hello, world! This is a test."
  14  	if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
  15  		t.Fatal(err)
  16  	}
  17  
  18  	e := PlainText{}
  19  	if !e.CanExtract(path) {
  20  		t.Error("should handle .txt files")
  21  	}
  22  
  23  	rc, err := e.Extract(path)
  24  	if err != nil {
  25  		t.Fatal(err)
  26  	}
  27  	defer rc.Close()
  28  
  29  	data, err := io.ReadAll(rc)
  30  	if err != nil {
  31  		t.Fatal(err)
  32  	}
  33  	if string(data) != content {
  34  		t.Errorf("got %q, want %q", string(data), content)
  35  	}
  36  }
  37  
  38  func TestPlainTextExtensions(t *testing.T) {
  39  	e := PlainText{}
  40  	tests := []struct {
  41  		path string
  42  		want bool
  43  	}{
  44  		{"foo.txt", true},
  45  		{"foo.md", true},
  46  		{"foo.text", true},
  47  		{"foo", true}, // no extension
  48  		{"foo.pdf", false},
  49  		{"foo.html", false},
  50  		{"foo.docx", false},
  51  	}
  52  	for _, tt := range tests {
  53  		if got := e.CanExtract(tt.path); got != tt.want {
  54  			t.Errorf("CanExtract(%q) = %v, want %v", tt.path, got, tt.want)
  55  		}
  56  	}
  57  }
  58  
  59  func TestPandocExtensions(t *testing.T) {
  60  	e := PandocExtractor{}
  61  	tests := []struct {
  62  		path string
  63  		want bool
  64  	}{
  65  		{"foo.html", true},
  66  		{"foo.htm", true},
  67  		{"foo.docx", true},
  68  		{"foo.epub", true},
  69  		{"foo.rtf", true},
  70  		{"foo.txt", false},
  71  		{"foo.pdf", false},
  72  	}
  73  	for _, tt := range tests {
  74  		if got := e.CanExtract(tt.path); got != tt.want {
  75  			t.Errorf("CanExtract(%q) = %v, want %v", tt.path, got, tt.want)
  76  		}
  77  	}
  78  }
  79  
  80  func TestPDFExtractorExtension(t *testing.T) {
  81  	e := PDFExtractor{}
  82  	if !e.CanExtract("book.pdf") {
  83  		t.Error("should handle .pdf")
  84  	}
  85  	if e.CanExtract("book.txt") {
  86  		t.Error("should not handle .txt")
  87  	}
  88  }
  89  
  90  func TestRegistryFallback(t *testing.T) {
  91  	dir := t.TempDir()
  92  	path := filepath.Join(dir, "test.txt")
  93  	if err := os.WriteFile(path, []byte("content"), 0o644); err != nil {
  94  		t.Fatal(err)
  95  	}
  96  
  97  	r := NewRegistry()
  98  	rc, err := r.Extract(path)
  99  	if err != nil {
 100  		t.Fatal(err)
 101  	}
 102  	rc.Close()
 103  }
 104  
 105  func TestIsTextFile(t *testing.T) {
 106  	dir := t.TempDir()
 107  
 108  	// Text file.
 109  	textPath := filepath.Join(dir, "text.txt")
 110  	if err := os.WriteFile(textPath, []byte("hello world"), 0o644); err != nil {
 111  		t.Fatal(err)
 112  	}
 113  	if !IsTextFile(textPath) {
 114  		t.Error("should detect as text")
 115  	}
 116  
 117  	// Binary file.
 118  	binPath := filepath.Join(dir, "binary.bin")
 119  	if err := os.WriteFile(binPath, []byte{0x00, 0x01, 0x02}, 0o644); err != nil {
 120  		t.Fatal(err)
 121  	}
 122  	if IsTextFile(binPath) {
 123  		t.Error("should detect as binary")
 124  	}
 125  }
 126