MoveFile { public MoveFile(@NotNull Project project, @NotNull P4CommandUtil cmd) { this.project = project; this.cmd = cmd; } MoveFile(@NotNull Project project, @NotNull P4CommandUtil cmd); File getExecDir(P4CommandRunner.ClientAction<?> action); MoveFileResult moveFile(IClient client, ClientConfig config, P4CommandRunner.ClientAction<?> baseType);  }
@ExtendWith(TemporaryFolderExtension.class) @Test void move_add(TemporaryFolder tmpDir) throws IOException { idea.useInlineThreading(null); MockConfigPart part = new MockConfigPart() .withServerName(server.getRshUrl()) .withUsername(server.getUser()) .withNoPassword() .withClientname("client1"); final ServerConfig serverConfig = ServerConfig.createFrom(part); final ClientConfig clientConfig = ClientConfig.createFrom(serverConfig, part); final File clientRoot = tmpDir.newFile("clientRoot"); final FilePath newFile = VcsUtil.getFilePath(touchFile(clientRoot, "abc.txt")); final FilePath toFile = VcsUtil.getFilePath(new File(clientRoot, "xyz.txt")); final P4CommandUtil cmd = new P4CommandUtil(); final MoveFile moveFile = new MoveFile(idea.getMockProject(), cmd); setupClient(clientConfig, tmpDir, clientRoot) .mapAsync((runner) -> runner.withConnection(clientConfig, (client) -> { List<IFileSpec> srcFiles = FileSpecBuildUtil.forFilePaths(newFile); List<IFileSpec> tgtFiles = FileSpecBuildUtil.forFilePaths(toFile); List<IFileSpec> msgs = cmd.addFiles(client, srcFiles, null, null, null); MessageStatusUtil.throwIfError(msgs); IChangelist change = client.getServer().getChangelist(IChangelist.DEFAULT); P4ChangelistId clId = createChangelistId(change, clientConfig); change.setDescription("add initial file"); msgs = cmd.submitChangelist(client, null, null, change, Collections.singletonList(newFile)); MessageStatusUtil.throwIfError(msgs); client.editFiles(srcFiles, new EditFilesOptions()); MoveFileResult res = moveFile.moveFile(client, clientConfig, new MoveFileAction(newFile, toFile, clId)); msgs = res.getServerMessages(); MessageStatusUtil.throwIfMessageOrEmpty("move", msgs); List<IFileSpec> specs = new ArrayList<>(2); specs.addAll(srcFiles); specs.addAll(tgtFiles); return cmd.getFileDetailsForOpenedSpecs(client.getServer(), specs, 10000); }) ) .whenCompleted((specs) -> { assertEquals(" assertEquals(" assertEquals(FileAction.MOVE_DELETE, specs.get(0).getAction()); assertEquals(FileAction.MOVE_ADD, specs.get(1).getAction()); }) .whenFailed(Assertions::fail); }
@ExtendWith(TemporaryFolderExtension.class) @Test void move_sourceEdited(TemporaryFolder tmpDir) throws IOException { idea.useInlineThreading(null); MockConfigPart part = new MockConfigPart() .withServerName(server.getRshUrl()) .withUsername(server.getUser()) .withNoPassword() .withClientname("client1"); final ServerConfig serverConfig = ServerConfig.createFrom(part); final ClientConfig clientConfig = ClientConfig.createFrom(serverConfig, part); final File clientRoot = tmpDir.newFile("clientRoot"); final FilePath fromFile = VcsUtil.getFilePath(touchFile(clientRoot, "abc.txt")); final FilePath toFile = VcsUtil.getFilePath(touchFile(clientRoot, "xyz.txt")); final P4CommandUtil cmd = new P4CommandUtil(); final MoveFile moveFile = new MoveFile(idea.getMockProject(), cmd); setContents(fromFile, "initial"); assertEquals("initial", getContents(fromFile)); assertTrue(toFile.getIOFile().delete()); setupClient(clientConfig, tmpDir, clientRoot) .mapAsync((runner) -> runner.withConnection(clientConfig, (client) -> { List<IFileSpec> srcFiles = FileSpecBuildUtil.forFilePaths(fromFile); List<IFileSpec> msgs = cmd.addFiles(client, srcFiles, null, null, null); MessageStatusUtil.throwIfError(msgs); IChangelist change = client.getServer().getChangelist(IChangelist.DEFAULT); P4ChangelistId clId = createChangelistId(change, clientConfig); change.setDescription("add initial file"); msgs = cmd.submitChangelist(client, null, null, change, Collections.singletonList(fromFile)); MessageStatusUtil.throwIfError(msgs); msgs = client.editFiles(srcFiles, new EditFilesOptions()); MessageStatusUtil.throwIfError(msgs); setContents(fromFile, "updated"); assertEquals("updated", getContents(fromFile)); MoveFileResult res = moveFile.moveFile(client, clientConfig, new MoveFileAction(fromFile, toFile, clId)); msgs = res.getServerMessages(); assertSize(1, msgs); assertNull(msgs.get(0).getStatusMessage()); assertEquals(toFile.getPath(), msgs.get(0).getLocalPathString()); assertTrue(fromFile.getIOFile().exists()); assertFalse(toFile.getIOFile().exists()); return null; }) ) .whenFailed(Assertions::fail); }
@ExtendWith(TemporaryFolderExtension.class) @Test void moveMessage_addNotOpen(TemporaryFolder tmpDir) throws IOException { idea.useInlineThreading(null); MockConfigPart part = new MockConfigPart() .withServerName(server.getRshUrl()) .withUsername(server.getUser()) .withNoPassword() .withClientname("client1"); final ServerConfig serverConfig = ServerConfig.createFrom(part); final ClientConfig clientConfig = ClientConfig.createFrom(serverConfig, part); final File clientRoot = tmpDir.newFile("clientRoot"); final FilePath fromFile = VcsUtil.getFilePath(touchFile(clientRoot, "abc.txt")); final FilePath toFile = VcsUtil.getFilePath(new File(clientRoot, "xyz.txt")); final P4CommandUtil cmd = new P4CommandUtil(); setupClient(clientConfig, tmpDir, clientRoot) .mapAsync((runner) -> runner.withConnection(clientConfig, (client) -> { List<IFileSpec> srcFiles = FileSpecBuildUtil.forFilePaths(fromFile); List<IFileSpec> tgtFiles = FileSpecBuildUtil.forFilePaths(toFile); List<IFileSpec> msgs = cmd.addFiles(client, srcFiles, null, null, null); MessageStatusUtil.throwIfError(msgs); IChangelist change = client.getServer().getChangelist(IChangelist.DEFAULT); change.setDescription("add initial file"); msgs = cmd.submitChangelist(client, null, null, change, Collections.singletonList(fromFile)); MessageStatusUtil.throwIfError(msgs); msgs = client.getServer().moveFile( srcFiles.get(0), tgtFiles.get(0), new MoveFileOptions()); assertSize(1, msgs); IServerMessage msg = msgs.get(0).getStatusMessage(); assertNotNull(msg); assertTrue(msg.isWarning()); Iterator<ISingleServerMessage> it = msg.getAllMessages().iterator(); assertTrue(it.hasNext()); ISingleServerMessage m = it.next(); assertFalse(it.hasNext()); assertEquals(m.getMessageFormat(), "[%argc% - file(s)|File(s)] not opened on this client."); assertEquals(382, m.getSubCode()); return null; }) ) .whenFailed(Assertions::fail); }
@ExtendWith(TemporaryFolderExtension.class) @Test void moveMessage_targetExists_notOpen(TemporaryFolder tmpDir) throws IOException { idea.useInlineThreading(null); MockConfigPart part = new MockConfigPart() .withServerName(server.getRshUrl()) .withUsername(server.getUser()) .withNoPassword() .withClientname("client1"); final ServerConfig serverConfig = ServerConfig.createFrom(part); final ClientConfig clientConfig = ClientConfig.createFrom(serverConfig, part); final File clientRoot = tmpDir.newFile("clientRoot"); final FilePath fromFile = VcsUtil.getFilePath(touchFile(clientRoot, "abc.txt")); final FilePath toFile = VcsUtil.getFilePath(touchFile(clientRoot, "xyz.txt")); final P4CommandUtil cmd = new P4CommandUtil(); setupClient(clientConfig, tmpDir, clientRoot) .mapAsync((runner) -> runner.withConnection(clientConfig, (client) -> { List<IFileSpec> srcFiles = FileSpecBuildUtil.forFilePaths(fromFile); List<IFileSpec> tgtFiles = FileSpecBuildUtil.forFilePaths(toFile); List<IFileSpec> allFiles = new ArrayList<>(2); allFiles.addAll(srcFiles); allFiles.addAll(tgtFiles); List<IFileSpec> msgs = cmd.addFiles(client, allFiles, null, null, null); MessageStatusUtil.throwIfError(msgs); IChangelist change = client.getServer().getChangelist(IChangelist.DEFAULT); change.setDescription("add initial file"); msgs = cmd.submitChangelist(client, null, null, change, Arrays.asList(fromFile, toFile)); MessageStatusUtil.throwIfError(msgs); msgs = client.editFiles(srcFiles, new EditFilesOptions()); MessageStatusUtil.throwIfError(msgs); assertTrue(toFile.getIOFile().setWritable(false)); msgs = client.getServer().moveFile( srcFiles.get(0), tgtFiles.get(0), new MoveFileOptions()); assertSize(1, msgs); IServerMessage msg = msgs.get(0).getStatusMessage(); assertNotNull(msg); assertTrue(msg.isInfo()); assertThat(msg.getAllInfoStrings(), containsString(" - is synced; use -f to force move")); Iterator<ISingleServerMessage> it = msg.getAllMessages().iterator(); assertTrue(it.hasNext()); ISingleServerMessage m = it.next(); assertFalse(it.hasNext()); assertEquals(530, m.getSubCode()); return null; }) ) .whenFailed(Assertions::fail); }
@ExtendWith(TemporaryFolderExtension.class) @Test void moveMessage_targetExists_notOpenForce(TemporaryFolder tmpDir) throws IOException { idea.useInlineThreading(null); MockConfigPart part = new MockConfigPart() .withServerName(server.getRshUrl()) .withUsername(server.getUser()) .withNoPassword() .withClientname("client1"); final ServerConfig serverConfig = ServerConfig.createFrom(part); final ClientConfig clientConfig = ClientConfig.createFrom(serverConfig, part); final File clientRoot = tmpDir.newFile("clientRoot"); final FilePath fromFile = VcsUtil.getFilePath(touchFile(clientRoot, "abc.txt")); final FilePath toFile = VcsUtil.getFilePath(touchFile(clientRoot, "xyz.txt")); final P4CommandUtil cmd = new P4CommandUtil(); setupClient(clientConfig, tmpDir, clientRoot, new MockP4RequestErrorHandler()) .mapAsync((runner) -> runner.withConnection(clientConfig, (client) -> { List<IFileSpec> srcFiles = FileSpecBuildUtil.forFilePaths(fromFile); List<IFileSpec> tgtFiles = FileSpecBuildUtil.forFilePaths(toFile); List<IFileSpec> allFiles = new ArrayList<>(2); allFiles.addAll(srcFiles); allFiles.addAll(tgtFiles); List<IFileSpec> msgs = cmd.addFiles(client, allFiles, null, null, null); MessageStatusUtil.throwIfError(msgs); IChangelist change = client.getServer().getChangelist(IChangelist.DEFAULT); change.setDescription("add initial file"); msgs = cmd.submitChangelist(client, null, null, change, Arrays.asList(fromFile, toFile)); MessageStatusUtil.throwIfError(msgs); msgs = client.editFiles(srcFiles, new EditFilesOptions()); MessageStatusUtil.throwIfError(msgs); assertTrue(toFile.getIOFile().setWritable(false)); msgs = client.getServer().moveFile( srcFiles.get(0), tgtFiles.get(0), new MoveFileOptions("-f")); fail("Should have thrown an error, returned messages " + msgs); return null; }) ) .whenFailed((t) -> { assertThat(t.getCause(), instanceOf(RequestException.class)); RequestException ex = (RequestException) t.getCause(); IServerMessage msg = ex.getServerMessage(); assertNotNull(msg); assertTrue(msg.isError()); Iterator<ISingleServerMessage> it = msg.getAllMessages().iterator(); assertTrue(it.hasNext()); ISingleServerMessage m = it.next(); assertFalse(it.hasNext()); assertEquals(m.getMessageFormat(), "Can't clobber writable file %file%"); assertEquals(MessageSubsystemCode.ES_DB, m.getSubCode()); }); }
@ExtendWith(TemporaryFolderExtension.class) @Test void moveMessage_targetExists_edit(TemporaryFolder tmpDir) throws IOException { idea.useInlineThreading(null); MockConfigPart part = new MockConfigPart() .withServerName(server.getRshUrl()) .withUsername(server.getUser()) .withNoPassword() .withClientname("client1"); final ServerConfig serverConfig = ServerConfig.createFrom(part); final ClientConfig clientConfig = ClientConfig.createFrom(serverConfig, part); final File clientRoot = tmpDir.newFile("clientRoot"); final FilePath fromFile = VcsUtil.getFilePath(touchFile(clientRoot, "abc.txt")); final FilePath toFile = VcsUtil.getFilePath(touchFile(clientRoot, "xyz.txt")); final P4CommandUtil cmd = new P4CommandUtil(); setupClient(clientConfig, tmpDir, clientRoot) .mapAsync((runner) -> runner.withConnection(clientConfig, (client) -> { List<IFileSpec> srcFiles = FileSpecBuildUtil.forFilePaths(fromFile); List<IFileSpec> tgtFiles = FileSpecBuildUtil.forFilePaths(toFile); List<IFileSpec> allFiles = new ArrayList<>(2); allFiles.addAll(srcFiles); allFiles.addAll(tgtFiles); List<IFileSpec> msgs = cmd.addFiles(client, allFiles, null, null, null); MessageStatusUtil.throwIfError(msgs); IChangelist change = client.getServer().getChangelist(IChangelist.DEFAULT); change.setDescription("add initial file"); msgs = cmd.submitChangelist(client, null, null, change, Arrays.asList(fromFile, toFile)); MessageStatusUtil.throwIfError(msgs); msgs = client.editFiles(allFiles, new EditFilesOptions()); MessageStatusUtil.throwIfError(msgs); msgs = client.getServer().moveFile( srcFiles.get(0), tgtFiles.get(0), new MoveFileOptions()); assertSize(1, msgs); IServerMessage msg = msgs.get(0).getStatusMessage(); assertNotNull(msg); assertTrue(msg.isInfo()); assertThat(msg.getAllInfoStrings(), containsString(" - can't move to an existing file")); Iterator<ISingleServerMessage> it = msg.getAllMessages().iterator(); assertTrue(it.hasNext()); ISingleServerMessage m = it.next(); assertFalse(it.hasNext()); assertEquals(IServerMessageCode.MOVE_EXISTS, m.getSubCode()); return null; }) ) .whenFailed(Assertions::fail); }