From 0bda7ed7445f30bdcb5c227f70cda390a5858d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=A6Ltorio?= Date: Tue, 30 Jul 2024 19:51:17 +0200 Subject: [PATCH] bug: verbose was ignored --- internal/translator/translator_test.go | 45 ++++++++++++++++++++++++-- main.go | 29 +++++++++++++++-- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/internal/translator/translator_test.go b/internal/translator/translator_test.go index 9c5476a..62a8105 100644 --- a/internal/translator/translator_test.go +++ b/internal/translator/translator_test.go @@ -1,3 +1,14 @@ +/* +Copyright (c) Ronan LE MEILLAT 2024 +Licensed under the AGPLv3 License +https://www.gnu.org/licenses/agpl-3.0.html + +Package translator provides functions for uploading, retrieving, and deleting files from Azure Blob Storage, +generating JSON documents for translation, and translating documents using the Azure Translator service. + +This file contains test functions for the various operations provided by the translator package. +*/ + package translator import ( @@ -8,11 +19,14 @@ import ( "testing" ) +// BLOB_NAME is the name of the blob file used for testing. const ( BLOB_NAME = "TestREADME.md" ) +// TestUploadFileToBlobStorage is a test function that tests the uploadFileToBlobStorage function. func TestUploadFileToBlobStorage(t *testing.T) { + // Test configuration config := TranslatorConfig{ TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"), TranslatorKey: os.Getenv("TRANSLATOR_KEY"), @@ -23,22 +37,29 @@ func TestUploadFileToBlobStorage(t *testing.T) { Timeout: 30, Verbose: true, } + + // File paths filePath := "../../README.md" blobName := BLOB_NAME + // Get absolute file path absFilePath, err := filepath.Abs(filePath) if err != nil { t.Fatalf("Failed to get absolute path: %v", err) } - err = uploadFileToBlobStorage(config, absFilePath, blobName) + // Upload file to blob storage + err = uploadFileToBlobStorage(config, absFilePath, blobName) if err != nil { t.Errorf("UploadFileToBlobStorage failed: %v", err) } // Add additional assertions or verifications here } + +// TestGetBlobURLWithSASToken is a test function that tests the getBlobURLWithSASToken function. func TestGetBlobURLWithSASToken(t *testing.T) { + // Test configuration config := TranslatorConfig{ TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"), TranslatorKey: os.Getenv("TRANSLATOR_KEY"), @@ -49,15 +70,18 @@ func TestGetBlobURLWithSASToken(t *testing.T) { Timeout: 30, Verbose: true, } + + // Blob name blobName := BLOB_NAME + // Get blob URL with SAS token urlWithSASToken, _, err := getBlobURLWithSASToken(config, blobName) fmt.Println(urlWithSASToken) if err != nil { t.Errorf("getBlobURLWithSASToken failed: %v", err) } - // test if the URL return 200 status code + // Test if the URL returns a 200 status code _, err = http.Get(urlWithSASToken) if err != nil { t.Errorf("getBlobURLWithSASToken failed: %v", err) @@ -66,7 +90,9 @@ func TestGetBlobURLWithSASToken(t *testing.T) { // Add additional assertions or verifications here } +// TestDeleteFileFromBlobStorage is a test function that tests the deleteFileFromBlobStorage function. func TestDeleteFileFromBlobStorage(t *testing.T) { + // Test configuration config := TranslatorConfig{ TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"), TranslatorKey: os.Getenv("TRANSLATOR_KEY"), @@ -77,8 +103,11 @@ func TestDeleteFileFromBlobStorage(t *testing.T) { Timeout: 30, Verbose: true, } + + // Blob name blobName := BLOB_NAME + // Delete file from blob storage err := deleteFileFromBlobStorage(config, blobName) if err != nil { t.Errorf("DeleteFileFromBlobStorage failed: %v", err) @@ -86,12 +115,16 @@ func TestDeleteFileFromBlobStorage(t *testing.T) { // Add additional assertions or verifications here } + +// TestGenerateJSONDocument is a test function that tests the generateJSONDocument function. func TestGenerateJSONDocument(t *testing.T) { + // Test data sourceSASUrl := "https://example.com/sourceSASUrl" targetSASUrl := "https://example.com/targetSASUrl" sourceLanguage := "en" targetLanguage := "fr" + // Expected JSON document expectedJSON := `{ "inputs": [ { @@ -110,23 +143,29 @@ func TestGenerateJSONDocument(t *testing.T) { ] }` + // Generate JSON document jsonDoc, err := generateJSONDocument(sourceSASUrl, targetSASUrl, sourceLanguage, targetLanguage) if err != nil { t.Errorf("generateJSONDocument failed: %v", err) } + // Compare generated JSON document with expected JSON document if jsonDoc != expectedJSON { t.Errorf("generateJSONDocument returned incorrect JSON.\nExpected:\n%s\n\nActual:\n%s", expectedJSON, jsonDoc) } // Add additional assertions or verifications here } + +// TestTranslateDocument is a test function that tests the TranslateDocument function. func TestTranslateDocument(t *testing.T) { + // Test data fileToTranslate := "../../README.md" fileTranslated := "../../README_fr.md" sourceLanguage := "en" targetLanguage := "fr" + // Test configuration config := TranslatorConfig{ TranslatorEndpoint: os.Getenv("TRANSLATOR_ENDPOINT"), TranslatorKey: os.Getenv("TRANSLATOR_KEY"), @@ -137,6 +176,8 @@ func TestTranslateDocument(t *testing.T) { Timeout: 30, Verbose: true, } + + // Translate document err := TranslateDocument(fileToTranslate, fileTranslated, sourceLanguage, targetLanguage, config) if err != nil { t.Errorf("TranslateDocument failed: %v", err) diff --git a/main.go b/main.go index b91fd4c..f68ede5 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,9 @@ +/* Copyright (c) Ronan LE MEILLAT 2024 + * Licensed under the AGPLv3 License + * https://www.gnu.org/licenses/agpl-3.0.html + */ +// Package main is the entry point for the AzurePDFTranslator application. +// It provides a command-line interface for translating PDF documents using Azure Translator and storing the translated documents in Azure Blob Storage. package main import ( @@ -9,6 +15,7 @@ import ( "translator/internal/translator" ) +// Constants for environment variable names const ( envTranslatorEndpoint = "TRANSLATOR_ENDPOINT" envTranslatorKey = "TRANSLATOR_KEY" @@ -20,6 +27,8 @@ const ( var verbose bool +// main is the entry point of the application. +// It parses command-line arguments, loads configuration from file if specified, validates inputs, and performs the translation. func main() { if err := run(); err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) @@ -27,7 +36,11 @@ func main() { } } +// run is the main logic of the application. +// It sets up the translator configuration, validates inputs, and performs the translation. +// It returns an error if any step fails. func run() error { + // Parse command-line arguments endpoint := flag.String("endpoint", os.Getenv(envTranslatorEndpoint), "Azure Translator API endpoint") key := flag.String("key", os.Getenv(envTranslatorKey), "Azure Translator API key") region := flag.String("region", os.Getenv(envTranslatorRegion), "Azure region") @@ -43,17 +56,20 @@ func run() error { flag.BoolVar(&verbose, "v", false, "enable verbose logging") flag.Parse() + // Load configuration from file if specified if *configFile != "" { - err := loadConfigFromFile(*configFile, endpoint, key, region, blobAccount, blobAccountKey, blobContainer) + err := loadConfigFromFile(*configFile, endpoint, key, region, blobAccount, blobAccountKey, blobContainer, &verbose) if err != nil { return err } } + // Validate inputs if err := validateInputs(*endpoint, *key, *region, *in, *out, *to, *blobAccount, *blobAccountKey, *blobContainer); err != nil { return err } + // Set up translator configuration config := translator.TranslatorConfig{ TranslatorEndpoint: *endpoint, TranslatorKey: *key, @@ -62,12 +78,16 @@ func run() error { BlobAccountKey: *blobAccountKey, BlobContainerName: *blobContainer, Timeout: *timeout, - Verbose: true, + Verbose: verbose, } + + // Perform the translation return translator.TranslateDocument(*in, *out, *from, *to, config) } -func loadConfigFromFile(configFile string, endpoint, key, region, blobAccount, blobAccountKey, blobContainer *string) error { +// loadConfigFromFile loads the translator configuration from a JSON file. +// It updates the endpoint, key, region, blobAccount, blobAccountKey, blobContainer, and verbose variables with the values from the file. +func loadConfigFromFile(configFile string, endpoint, key, region, blobAccount, blobAccountKey, blobContainer *string, verbose *bool) error { file, err := os.Open(configFile) if err != nil { return err @@ -86,10 +106,13 @@ func loadConfigFromFile(configFile string, endpoint, key, region, blobAccount, b *blobAccount = config.BlobAccountName *blobAccountKey = config.BlobAccountKey *blobContainer = config.BlobContainerName + *verbose = config.Verbose return nil } +// validateInputs checks if all the required inputs are provided. +// It returns an error if any required input is missing. func validateInputs(endpoint, key, region, in, out, to, blobAccount, blobAccountKey, blobContainer string) error { missingArgs := []string{} if endpoint == "" {