Commit cdc15ad9 authored by Yuanle Song's avatar Yuanle Song
Browse files

implement ToMingwPath

added unit test sub project.
parent 0b533ed7
Loading
Loading
Loading
Loading
+28 −10
Original line number Diff line number Diff line
@@ -8,8 +8,11 @@
//     /%appdata%/mbackup/mbackup-default.exclude
//     /%appdata%/mbackup/local.exclude (optional)

module Mbackup

open System
open System.Diagnostics
open System.Text.RegularExpressions;

open Argu

@@ -69,16 +72,31 @@ let GetEnvDefault (varName: string) (defaultValue: string) =
    | "" -> defaultValue
    | _ -> value

// Convert windows path to Mingw64 path
let ToMingwPath windowsPath =
  // TODO implement me
// Convert windows path to Mingw64 path.
// Supported windows path: C:\foo, C:/foo, /c/foo
// MingwPath format: /cygdrive/c/foo
let ToMingwPath (windowsPath: string) =
  let pattern = Regex("^/([c-zC-Z])/", RegexOptions.None)
  let result =
    if pattern.IsMatch(windowsPath) then
      "/cygdrive" + windowsPath
    else
      let pattern = Regex("^([c-zC-Z]):", RegexOptions.None)
      if pattern.IsMatch(windowsPath) then
        let result = windowsPath.Replace('\\', '/')
        "/cygdrive/" + result.Substring(0, 1).ToLower() + result.Substring(2)
      else
        windowsPath
  result

let EnsureDir (path: string) = if path.EndsWith "/" then path else path + "/"

let appDataRoamingDir = ToMingwPath (Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
let appDataLocalDir = ToMingwPath (Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
let appDataRoamingDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) |> ToMingwPath |> EnsureDir
let programDataDir = GetEnv "PROGRAMDATA" |> ToMingwPath |> EnsureDir
let appDataLocalDir =  Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) |> ToMingwPath |> EnsureDir

// TODO make sure dir ends with /
let userConfigDir = appDataRoamingDir + "/mbackup/"
//let userConfigDir = appDataRoamingDir + "mbackup/"
let userConfigDir = programDataDir + "mbackup/"
let runtimeDir = appDataLocalDir

let isLocalTarget (target: string) = target.StartsWith "/"

git-hooks/pre-commit

0 → 100644
+2 −0
Original line number Diff line number Diff line
cd mbackup-tests
dotnet test
+1 −0
Original line number Diff line number Diff line
# dirs to backup
/cygdrive/c/ProgramData/mbackup
/cygdrive/c/path/to/dir
/cygdrive/d/path/to/dir
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <RootNamespace>mbackup_for_windows</RootNamespace>
    <RootNamespace>Mbackup</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
+27 −0
Original line number Diff line number Diff line
module MbackupTests

open NUnit.Framework
open Mbackup

[<SetUp>]
let Setup () =
    ()

[<Test>]
let TestDumb () =
    Assert.Pass()

[<Test>]
let TestToMingwPath () =
    Assert.That("/cygdrive/c/", Is.EqualTo(ToMingwPath "c:\\"))
    Assert.That("/cygdrive/c/", Is.EqualTo(ToMingwPath "C:\\"))
    Assert.That("/cygdrive/c/foo", Is.EqualTo(ToMingwPath "C:\\foo"))
    Assert.That("/cygdrive/d/foo", Is.EqualTo(ToMingwPath "D:\\foo"))
    Assert.That("/cygdrive/d/Foo", Is.EqualTo(ToMingwPath "D:\\Foo"))
    Assert.That("/cygdrive/c/foo/bar/", Is.EqualTo(ToMingwPath "C:\\foo\\bar\\"))
    Assert.That("/cygdrive/c/foo/bar/baz.txt", Is.EqualTo(ToMingwPath "C:\\foo\\bar\\baz.txt"))
    Assert.That("/cygdrive/c/foo", Is.EqualTo(ToMingwPath "C:/foo"))
    Assert.That("/cygdrive/c/foo/bar", Is.EqualTo(ToMingwPath "C:/foo/bar"))
    Assert.That("/cygdrive/c/foo", Is.EqualTo(ToMingwPath "/c/foo"))
    Assert.That("/cygdrive/D/foo", Is.EqualTo(ToMingwPath "/D/foo"))
    Assert.That("/var/log", Is.EqualTo(ToMingwPath "/var/log"))
Loading