test_linux_nocgo.sh 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/bash
  2. # Test script for Linux CGO-free clipboard implementation
  3. echo "Testing Linux clipboard implementation without CGO..."
  4. # Check for required tools
  5. echo "Checking for clipboard tools..."
  6. for tool in xclip xsel wl-copy; do
  7. if command -v $tool &> /dev/null; then
  8. echo "✓ $tool is installed"
  9. else
  10. echo "✗ $tool is not installed"
  11. fi
  12. done
  13. # Create test program
  14. cat > test_linux_clipboard.go << 'EOF'
  15. package main
  16. import (
  17. "fmt"
  18. "log"
  19. "os"
  20. "golang.design/x/clipboard"
  21. )
  22. func main() {
  23. err := clipboard.Init()
  24. if err != nil {
  25. log.Fatal("Failed to initialize clipboard:", err)
  26. }
  27. // Test text
  28. fmt.Println("\n=== Testing Text Clipboard ===")
  29. testText := []byte("Hello from CGO-free Linux clipboard!")
  30. clipboard.Write(clipboard.FmtText, testText)
  31. fmt.Println("Wrote text:", string(testText))
  32. readText := clipboard.Read(clipboard.FmtText)
  33. fmt.Println("Read text:", string(readText))
  34. if string(testText) == string(readText) {
  35. fmt.Println("✓ Text clipboard test passed")
  36. } else {
  37. fmt.Println("✗ Text clipboard test failed")
  38. }
  39. // Test empty write
  40. fmt.Println("\n=== Testing Empty Write ===")
  41. clipboard.Write(clipboard.FmtText, []byte{})
  42. emptyRead := clipboard.Read(clipboard.FmtText)
  43. if emptyRead == nil || len(emptyRead) == 0 {
  44. fmt.Println("✓ Empty write test passed")
  45. } else {
  46. fmt.Println("✗ Empty write test failed, got:", string(emptyRead))
  47. }
  48. // Test image if requested
  49. if len(os.Args) > 1 && os.Args[1] == "image" {
  50. fmt.Println("\n=== Testing Image Clipboard ===")
  51. // Try to read test image
  52. imageData, err := os.ReadFile("tests/testdata/clipboard.png")
  53. if err != nil {
  54. fmt.Println("Could not read test image:", err)
  55. return
  56. }
  57. clipboard.Write(clipboard.FmtImage, imageData)
  58. fmt.Println("Wrote image data, length:", len(imageData))
  59. readImage := clipboard.Read(clipboard.FmtImage)
  60. if readImage != nil {
  61. fmt.Println("Read image data, length:", len(readImage))
  62. if len(imageData) == len(readImage) {
  63. fmt.Println("✓ Image clipboard test passed")
  64. } else {
  65. fmt.Println("✗ Image lengths don't match")
  66. }
  67. } else {
  68. fmt.Println("✗ Failed to read image from clipboard")
  69. }
  70. // Test that reading text from image clipboard returns nil
  71. textFromImage := clipboard.Read(clipboard.FmtText)
  72. if textFromImage == nil {
  73. fmt.Println("✓ Reading text from image clipboard correctly returned nil")
  74. } else {
  75. fmt.Println("✗ Reading text from image clipboard should return nil, got:", string(textFromImage))
  76. }
  77. }
  78. }
  79. EOF
  80. # Run tests with CGO disabled
  81. echo -e "\n=== Running with CGO_ENABLED=0 ==="
  82. CGO_ENABLED=0 go run test_linux_clipboard.go
  83. echo -e "\n=== Running with CGO_ENABLED=0 and image test ==="
  84. CGO_ENABLED=0 go run test_linux_clipboard.go image
  85. # Run actual tests
  86. echo -e "\n=== Running go test with CGO_ENABLED=0 ==="
  87. CGO_ENABLED=0 go test -v -run TestClipboard
  88. # Clean up
  89. rm -f test_linux_clipboard.go
  90. echo -e "\nTest script completed!"