public void set_WebRoot()
        {
            TM_FileStorage.Custom_WebRoot = null;
            var custom_WebRoot            = "Custom_WebRoot".tempDir().assert_Folder_Exists();
            var expected_Path_XmlDatabase = custom_WebRoot.pathCombine(@"App_Data\TeamMentor");

            UserRole.Admin.assert();

            TM_FileStorage.Custom_WebRoot.assert_Null();
            var tmFileStorage = new TM_FileStorage(loadData: false);
            tmFileStorage.webRoot().assert_Null();
            tmFileStorage.set_WebRoot();
            tmFileStorage.webRoot().assert_Not_Null()
                                   .assert_Equal(AppDomain.CurrentDomain.BaseDirectory);

            TM_FileStorage.Custom_WebRoot = custom_WebRoot;
            tmFileStorage.webRoot().assert_Equal(AppDomain.CurrentDomain.BaseDirectory)     // should still point to the AppDomain base directory
                                   .assert_Not_Equal(custom_WebRoot);

            tmFileStorage.using_Custom_WebRoot().assert_False();                            // this should only be true when the values match

            tmFileStorage.set_WebRoot()                                                     // set WebRoot
                         .webRoot().assert_Equals(custom_WebRoot);                          // and confirm its location

            tmFileStorage.using_Custom_WebRoot().assert_True();                             // now it should be true
            tmFileStorage.path_XmlDatabase().assert_Null();                                 // confirm that not set
            tmFileStorage.set_Path_XmlDatabase();                                           // this should set the TM_Xml_Database inside the Web_Root
            tmFileStorage.path_XmlDatabase().contains(custom_WebRoot);
            tmFileStorage.path_XmlDatabase().assert_Is_Equal_To(expected_Path_XmlDatabase); // check that the current Path_XmlDatabase matches the expected location

            //reset values
            TM_FileStorage.Custom_WebRoot = null;

            tmFileStorage.set_WebRoot()
                         .webRoot().assert_Not_Equals(custom_WebRoot);

            Files.delete_Folder_Recursively(custom_WebRoot).assert_True();
            custom_WebRoot.assert_Folder_Doesnt_Exist();
        }
Beispiel #2
0
        /// <summary>
        /// Sets (after calculation) the value of tmFileStorage.Path_XmlDatabas
        ///
        /// The logic is a bit complicated since it takes into account the different execution locations of NCrunch, Resharper and IIS
        ///
        /// There is support for using the special ASP.NET App_Data folder (also used when the current running used does not have priviledges
        /// the mapped tmFileStorage.Path_XmlDatabase value)
        /// </summary>
        /// <param name="tmFileStorage"></param>
        /// <returns></returns>

        [Admin] public static TM_FileStorage   set_Path_XmlDatabase(this TM_FileStorage tmFileStorage)
        {
            admin.demand();
            var tmStatus = TM_Status.Current;

            try
            {
                if (tmFileStorage.isNull())
                {
                    return(null);
                }

                if (TM_FileStorage.Custom_Path_XmlDatabase.folder_Exists())
                {
                    "[TM_Server][set_Path_XmlDatabase] using TM_FileStorage.Custom_Path_XmlDatabase value".info();
                    tmFileStorage.Path_XmlDatabase = TM_FileStorage.Custom_Path_XmlDatabase;
                    return(tmFileStorage);
                }

                var webRoot = tmFileStorage.WebRoot;

                tmFileStorage.Path_XmlDatabase = null;

                var usingAppData = webRoot.contains(@"TeamMentor.UnitTests\bin") ||             // when running UnitTests under NCrunch
                                   webRoot.contains(@"site\wwwroot") ||                         // when running from Azure (or directly on IIS)
                                   tmFileStorage.using_Custom_WebRoot();                        // when the TM_FileStorage.Custom_WebRoot has been set


                if (usingAppData.isFalse())
                {
                    //calculate location and see if we can write to it

                    var xmlDatabasePath = webRoot.pathCombine(TMConsts.VIRTUAL_PATH_MAPPING)
                                          .pathCombine(TMConsts.XML_DATABASE_VIRTUAL_PATH_LEGACY)          //use by default the 'Library_Data\\XmlDatabase" value due to legacy support (pre 3.3)
                                          .fullPath();

                    if (xmlDatabasePath.createDir().dirExists() && xmlDatabasePath.canWriteToPath())
                    {
                        tmFileStorage.Path_XmlDatabase = xmlDatabasePath;                        // if can write it then make it the Path_XmlDatabase
                        tmStatus.TM_Database_Location_Using_AppData = false;
                        return(tmFileStorage);
                    }
                    "[TM_Server][set_Path_XmlDatabase] It was not possible to write to mapped folder: {0}".error(xmlDatabasePath);
                }

                var appData_Path = webRoot.pathCombine("App_Data")
                                   .pathCombine(TMConsts.XML_DATABASE_VIRTUAL_PATH)               // inside App_Data we can use the folder value 'TeamMentor'
                                   .fullPath();
                if (appData_Path.createDir().dirExists() && appData_Path.canWriteToPath())
                {
                    tmFileStorage.Path_XmlDatabase = appData_Path;                        // if can write it then make it the Path_XmlDatabase
                    tmStatus.TM_Database_Location_Using_AppData = true;
                    return(tmFileStorage);
                }

                "[TM_Server][set_Path_XmlDatabase] It was not possible to write to App_Data folder: {0}".error(appData_Path);
                //TM_Server.UseFileStorage = false;
                return(tmFileStorage);
            }
            finally
            {
                "[TM_Server][set_Path_XmlDatabase] Path_XmlDatabase set to: {0}".info(tmFileStorage.Path_XmlDatabase);
                "[TM_Server][set_Path_XmlDatabase] tmStatus.TM_Database_Location_Using_AppData:{0}".info(tmStatus.TM_Database_Location_Using_AppData);
            }
        }