@@ -56,7 +56,7 @@ import util::Reflective;
5656
5757//// Fixtures and utility functions
5858data TestModule = byText (str name , str body , set [int ] nameOccs , str newName = name , set [int ] skipCursors = {})
59- | byLoc (loc file , set [int ] nameOccs , str newName = name , set [int ] skipCursors = {});
59+ | byLoc (str name , loc file , set [int ] nameOccs , str newName = name , set [int ] skipCursors = {});
6060
6161private list [DocumentEdit ] sortEdits (list [DocumentEdit ] edits ) = [sortChanges (e ) | e <- edits ];
6262
@@ -68,10 +68,20 @@ private default DocumentEdit sortChanges(DocumentEdit e) = e;
6868private void verifyTypeCorrectRenaming (loc root , Edits edits , PathConfig pcfg ) {
6969 list [loc ] editLocs = [l | /replace(l, _) := edits<0 > ];
7070 assert size(editLocs) == size(toSet(editLocs)) : "Duplicate locations in suggested edits - VS Code cannot handle this";
71+
72+ // Back-up sources
73+ loc backupLoc = |memory://tests/ backup |;
74+ remove (backupLoc , recursive = true );
75+ copy (root , backupLoc , recursive = true );
76+
7177 executeDocumentEdits (sortEdits (edits <0 >));
7278 remove (pcfg .resources );
7379 RascalCompilerConfig ccfg = rascalCompilerConfig (pcfg )[verbose = false ][logPathConfig = false ];
7480 throwAnyErrors (checkAll (root , ccfg ));
81+
82+ // Restore back-up
83+ remove (root , recursive = true );
84+ move (backupLoc , root , overwrite = true );
7585}
7686
7787bool expectEq (&T expected , &T actual , str epilogue = "" ) {
@@ -92,29 +102,31 @@ bool expectEq(&T expected, &T actual, str epilogue = "") {
92102
93103bool testRenameOccurrences (set [TestModule ] modules , str oldName = "foo" , str newName = "bar" ) {
94104 bool success = true ;
95- for (mm <- modules , cursorOcc <- (mm .nameOccs - mm .skipCursors )) {
96- str testName = "Test_<mm .name > _<cursorOcc > " ;
97- loc testDir = |memory ://tests/rename/<testName>|;
98105
99- if (any (m <- modules , m is byLoc )) {
100- testDir = cover ([m .file | m <- modules , m is byLoc ]);
106+ bool moduleExistsOnDisk = any (mmm <- modules , mmm is byLoc );
107+ for (mm <- modules , cursorOcc <- (mm .nameOccs - mm .skipCursors )) {
108+ loc testDir = |unknown:///| ;
109+ if (moduleExistsOnDisk ){
110+ testDir = cover ([m .file .parent | m <- modules , m is byLoc ]).parent ;
101111 } else {
102112 // If none of the modules refers to an existing file, clear the test directory before writing files.
113+ str testName = "Test_<mm .name > _<cursorOcc > " ;
114+ testDir = |memory ://tests/rename/<testName>|;
103115 remove (testDir );
104116 }
105117
106118 pcfg = getTestPathConfig (testDir );
107- modulesByLocation = {mByLoc | m <- modules , mByLoc := (m is byLoc ? m : byLoc (storeTestModule (testDir , m .name , m .body ), m .nameOccs , newName = m .newName , skipCursors = m .skipCursors ))};
119+ modulesByLocation = {mByLoc | m <- modules , mByLoc := (m is byLoc ? m : byLoc (m . name , storeTestModule (testDir , m .name , m .body ), m .nameOccs , newName = m .newName , skipCursors = m .skipCursors ))};
108120
109- for (byLoc ( loc ml , _) <- modulesByLocation ) {
121+ for (m <- modulesByLocation ) {
110122 try {
111- parseModuleWithSpaces (ml );
123+ parseModuleWithSpaces (m . file );
112124 } catch _: {
113125 throw "Parse error in test module <ml > " ;
114126 }
115127 }
116128
117- cursorT = findCursor ([m .file | m <- modulesByLocation , getModuleName ( m . file , pcfg ) == mm .name ][0 ], oldName , cursorOcc );
129+ cursorT = findCursor ([m .file | m <- modulesByLocation , m . name == mm .name ][0 ], oldName , cursorOcc );
118130
119131 println ("Renaming \' <oldName > \' from <cursorT .src > " );
120132 edits = rascalRenameSymbol (cursorT , toSet (pcfg .srcs ), newName , PathConfig (loc _) { return pcfg ; });
@@ -149,11 +161,13 @@ bool testRenameOccurrences(set[TestModule] modules, str oldName = "foo", str new
149161 success = false ;
150162 }
151163
152- for (success , src <- pcfg . srcs ) {
153- verifyTypeCorrectRenaming (src , edits , pcfg );
164+ if (success ) {
165+ verifyTypeCorrectRenaming (testDir , edits , pcfg );
154166 }
155167
156- remove (testDir );
168+ if (!moduleExistsOnDisk ) {
169+ remove (testDir );
170+ }
157171 }
158172
159173 return success ;
@@ -186,7 +200,7 @@ bool testRename(str stmtsStr, int cursorAtOldNameOccurrence = 0, str oldName = "
186200 return false ;
187201}
188202
189- private PathConfig getTestPathConfig(loc testDir) {
203+ public PathConfig getTestPathConfig (loc testDir ) {
190204 return pathConfig (
191205 bin =testDir + "bin" ,
192206 libs =[|lib://rascal| ],
0 commit comments