添加项目文件。
This commit is contained in:
79
BackupTest.cs
Normal file
79
BackupTest.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
|
||||
namespace DrillTools
|
||||
{
|
||||
/// <summary>
|
||||
/// 备份功能测试类
|
||||
/// </summary>
|
||||
public static class BackupTest
|
||||
{
|
||||
/// <summary>
|
||||
/// 测试备份功能
|
||||
/// </summary>
|
||||
public static void TestBackupFunctionality()
|
||||
{
|
||||
Console.WriteLine("=== 备份功能测试 ===");
|
||||
|
||||
// 创建测试文件
|
||||
string testFilePath = "test_file.drl";
|
||||
string testContent = "M48\nT01C1.000\n%\nT01\nX1000Y1000\nM30";
|
||||
|
||||
try
|
||||
{
|
||||
// 1. 创建原始测试文件
|
||||
File.WriteAllText(testFilePath, testContent);
|
||||
Console.WriteLine("✓ 创建测试文件成功");
|
||||
|
||||
// 2. 第一次备份(应该直接创建.bak文件)
|
||||
string backupPath1 = testFilePath + ".bak";
|
||||
if (File.Exists(backupPath1))
|
||||
File.Delete(backupPath1);
|
||||
|
||||
File.Copy(testFilePath, backupPath1);
|
||||
Console.WriteLine("✓ 第一次备份成功");
|
||||
|
||||
// 3. 模拟第二次备份(检测到.bak文件已存在)
|
||||
if (File.Exists(backupPath1))
|
||||
{
|
||||
Console.WriteLine("✓ 检测到备份文件已存在");
|
||||
|
||||
// 模拟用户选择创建时间戳备份
|
||||
string timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
|
||||
string timestampBackupPath = $"{testFilePath}.{timestamp}.bak";
|
||||
File.Copy(testFilePath, timestampBackupPath);
|
||||
Console.WriteLine($"✓ 创建时间戳备份成功: {Path.GetFileName(timestampBackupPath)}");
|
||||
}
|
||||
|
||||
// 4. 验证备份文件内容
|
||||
if (File.Exists(backupPath1) && File.ReadAllText(backupPath1) == testContent)
|
||||
{
|
||||
Console.WriteLine("✓ 备份文件内容验证成功");
|
||||
}
|
||||
|
||||
// 5. 清理测试文件
|
||||
File.Delete(testFilePath);
|
||||
if (File.Exists(backupPath1))
|
||||
File.Delete(backupPath1);
|
||||
|
||||
// 删除时间戳备份文件
|
||||
var timestampFiles = Directory.GetFiles(".", "*.bak");
|
||||
foreach (var file in timestampFiles)
|
||||
{
|
||||
if (file.Contains("test_file.") && file.Contains(".bak"))
|
||||
{
|
||||
File.Delete(file);
|
||||
Console.WriteLine($"✓ 清理测试文件: {Path.GetFileName(file)}");
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("=== 备份功能测试完成 ===");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"✗ 测试失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user