OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Unit tests for gclient_scm.py.""" | 6 """Unit tests for gclient_scm.py.""" |
7 | 7 |
8 # pylint: disable=E1103 | 8 # pylint: disable=E1103 |
9 | 9 |
10 # Import before super_mox to keep valid references. | 10 # Import before super_mox to keep valid references. |
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
807 relpath=self.relpath) | 807 relpath=self.relpath) |
808 file_list = [] | 808 file_list = [] |
809 scm.update(options, None, file_list) | 809 scm.update(options, None, file_list) |
810 gclient_scm.os.remove(file_path) | 810 gclient_scm.os.remove(file_path) |
811 file_list = [] | 811 file_list = [] |
812 scm.revert(options, self.args, file_list) | 812 scm.revert(options, self.args, file_list) |
813 self.assertEquals(file_list, [file_path]) | 813 self.assertEquals(file_list, [file_path]) |
814 file_list = [] | 814 file_list = [] |
815 scm.diff(options, self.args, file_list) | 815 scm.diff(options, self.args, file_list) |
816 self.assertEquals(file_list, []) | 816 self.assertEquals(file_list, []) |
817 expectation = ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n' | |
818 'Fast-forward\n a | 1 +\n b | 1 +\n' | |
819 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n' | |
820 '________ running \'git reset --hard origin/master\' in \'%s\'\n' | |
821 'HEAD is now at a7142dc Personalized\n') % join(self.root_dir, '.') | |
822 self.assertTrue(sys.stdout.getvalue().startswith(expectation)) | |
823 sys.stdout.close() | 817 sys.stdout.close() |
824 | 818 |
825 def testRevertNone(self): | 819 def testRevertNone(self): |
826 if not self.enabled: | 820 if not self.enabled: |
827 return | 821 return |
828 options = self.Options() | 822 options = self.Options() |
829 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 823 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
830 relpath=self.relpath) | 824 relpath=self.relpath) |
831 file_list = [] | 825 file_list = [] |
832 scm.update(options, None, file_list) | 826 scm.update(options, None, file_list) |
833 file_list = [] | 827 file_list = [] |
834 scm.revert(options, self.args, file_list) | 828 scm.revert(options, self.args, file_list) |
835 self.assertEquals(file_list, []) | 829 self.assertEquals(file_list, []) |
836 self.assertEquals(scm.revinfo(options, self.args, None), | 830 self.assertEquals(scm.revinfo(options, self.args, None), |
837 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 831 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
838 self.checkstdout( | 832 sys.stdout.close() |
839 ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n' | |
840 'Fast-forward\n a | 1 +\n b | 1 +\n' | |
841 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n' | |
842 '________ running \'git reset --hard origin/master\' in \'%s\'\n' | |
843 'HEAD is now at a7142dc Personalized\n') % | |
844 join(self.root_dir, '.')) | |
845 | 833 |
846 def testRevertModified(self): | 834 def testRevertModified(self): |
847 if not self.enabled: | 835 if not self.enabled: |
848 return | 836 return |
849 options = self.Options() | 837 options = self.Options() |
850 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 838 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
851 relpath=self.relpath) | 839 relpath=self.relpath) |
852 file_list = [] | 840 file_list = [] |
853 scm.update(options, None, file_list) | 841 scm.update(options, None, file_list) |
854 file_path = join(self.base_path, 'a') | 842 file_path = join(self.base_path, 'a') |
855 open(file_path, 'a').writelines('touched\n') | 843 open(file_path, 'a').writelines('touched\n') |
856 file_list = [] | 844 file_list = [] |
857 scm.revert(options, self.args, file_list) | 845 scm.revert(options, self.args, file_list) |
858 self.assertEquals(file_list, [file_path]) | 846 self.assertEquals(file_list, [file_path]) |
859 file_list = [] | 847 file_list = [] |
860 scm.diff(options, self.args, file_list) | 848 scm.diff(options, self.args, file_list) |
861 self.assertEquals(file_list, []) | 849 self.assertEquals(file_list, []) |
862 self.assertEquals(scm.revinfo(options, self.args, None), | 850 self.assertEquals(scm.revinfo(options, self.args, None), |
863 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 851 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
864 expectation = ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n' | |
865 'Fast-forward\n a | 1 +\n b | 1 +\n' | |
866 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n' | |
867 '________ running \'git reset --hard origin/master\' in \'%s\'\n' | |
868 'HEAD is now at a7142dc Personalized\n') % join(self.root_dir, '.') | |
869 self.assertTrue(sys.stdout.getvalue().startswith(expectation)) | |
870 sys.stdout.close() | 852 sys.stdout.close() |
871 | 853 |
872 def testRevertNew(self): | 854 def testRevertNew(self): |
873 if not self.enabled: | 855 if not self.enabled: |
874 return | 856 return |
875 options = self.Options() | 857 options = self.Options() |
876 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 858 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
877 relpath=self.relpath) | 859 relpath=self.relpath) |
878 file_list = [] | 860 file_list = [] |
879 scm.update(options, None, file_list) | 861 scm.update(options, None, file_list) |
880 file_path = join(self.base_path, 'c') | 862 file_path = join(self.base_path, 'c') |
881 f = open(file_path, 'w') | 863 f = open(file_path, 'w') |
882 f.writelines('new\n') | 864 f.writelines('new\n') |
883 f.close() | 865 f.close() |
884 Popen(['git', 'add', 'c'], stdout=PIPE, | 866 Popen(['git', 'add', 'c'], stdout=PIPE, |
885 stderr=STDOUT, cwd=self.base_path).communicate() | 867 stderr=STDOUT, cwd=self.base_path).communicate() |
886 file_list = [] | 868 file_list = [] |
887 scm.revert(options, self.args, file_list) | 869 scm.revert(options, self.args, file_list) |
888 self.assertEquals(file_list, [file_path]) | 870 self.assertEquals(file_list, [file_path]) |
889 file_list = [] | 871 file_list = [] |
890 scm.diff(options, self.args, file_list) | 872 scm.diff(options, self.args, file_list) |
891 self.assertEquals(file_list, []) | 873 self.assertEquals(file_list, []) |
892 self.assertEquals(scm.revinfo(options, self.args, None), | 874 self.assertEquals(scm.revinfo(options, self.args, None), |
893 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 875 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
894 expectation = ('\n_____ . at refs/heads/master\nUpdating 069c602..a7142dc\n' | |
895 'Fast-forward\n a | 1 +\n b | 1 +\n' | |
896 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n\n' | |
897 '________ running \'git reset --hard origin/master\' in \'%s\'\n' | |
898 'HEAD is now at a7142dc Personalized\n') % join(self.root_dir, '.') | |
899 self.assertTrue(sys.stdout.getvalue().startswith(expectation)) | |
900 sys.stdout.close() | 876 sys.stdout.close() |
901 | 877 |
902 def testStatusNew(self): | 878 def testStatusNew(self): |
903 if not self.enabled: | 879 if not self.enabled: |
904 return | 880 return |
905 options = self.Options() | 881 options = self.Options() |
906 file_path = join(self.base_path, 'a') | 882 file_path = join(self.base_path, 'a') |
907 open(file_path, 'a').writelines('touched\n') | 883 open(file_path, 'a').writelines('touched\n') |
908 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 884 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
909 relpath=self.relpath) | 885 relpath=self.relpath) |
(...skipping 30 matching lines...) Expand all Loading... |
940 return | 916 return |
941 options = self.Options() | 917 options = self.Options() |
942 expected_file_list = [join(self.base_path, x) for x in ['a', 'b']] | 918 expected_file_list = [join(self.base_path, x) for x in ['a', 'b']] |
943 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 919 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
944 relpath=self.relpath) | 920 relpath=self.relpath) |
945 file_list = [] | 921 file_list = [] |
946 scm.update(options, (), file_list) | 922 scm.update(options, (), file_list) |
947 self.assertEquals(file_list, expected_file_list) | 923 self.assertEquals(file_list, expected_file_list) |
948 self.assertEquals(scm.revinfo(options, (), None), | 924 self.assertEquals(scm.revinfo(options, (), None), |
949 'a7142dc9f0009350b96a11f372b6ea658592aa95') | 925 'a7142dc9f0009350b96a11f372b6ea658592aa95') |
950 self.checkstdout( | 926 sys.stdout.close() |
951 '\n_____ . at refs/heads/master\n' | |
952 'Updating 069c602..a7142dc\nFast-forward\n a | 1 +\n b | 1 +\n' | |
953 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n') | |
954 | 927 |
955 def testUpdateReset(self): | 928 def testUpdateReset(self): |
956 if not self.enabled: | 929 if not self.enabled: |
957 return | 930 return |
958 options = self.Options() | 931 options = self.Options() |
959 options.reset = True | 932 options.reset = True |
960 | 933 |
961 dir_path = join(self.base_path, 'c') | 934 dir_path = join(self.base_path, 'c') |
962 os.mkdir(dir_path) | 935 os.mkdir(dir_path) |
963 open(join(dir_path, 'nested'), 'w').writelines('new\n') | 936 open(join(dir_path, 'nested'), 'w').writelines('new\n') |
964 | 937 |
965 file_path = join(self.base_path, 'file') | 938 file_path = join(self.base_path, 'file') |
966 open(file_path, 'w').writelines('new\n') | 939 open(file_path, 'w').writelines('new\n') |
967 | 940 |
968 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 941 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
969 relpath=self.relpath) | 942 relpath=self.relpath) |
970 file_list = [] | 943 file_list = [] |
971 scm.update(options, (), file_list) | 944 scm.update(options, (), file_list) |
972 self.assert_(gclient_scm.os.path.isdir(dir_path)) | 945 self.assert_(gclient_scm.os.path.isdir(dir_path)) |
973 self.assert_(gclient_scm.os.path.isfile(file_path)) | 946 self.assert_(gclient_scm.os.path.isfile(file_path)) |
974 self.checkstdout( | 947 sys.stdout.close() |
975 '\n________ running \'git reset --hard HEAD\' in \'%s\'' | |
976 '\nHEAD is now at 069c602 A and B\n' | |
977 '\n_____ . at refs/heads/master\n' | |
978 'Updating 069c602..a7142dc\nFast-forward\n a | 1 +\n b | 1 +\n' | |
979 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n' | |
980 % join(self.root_dir, '.')) | |
981 | 948 |
982 def testUpdateResetDeleteUnversionedTrees(self): | 949 def testUpdateResetDeleteUnversionedTrees(self): |
983 if not self.enabled: | 950 if not self.enabled: |
984 return | 951 return |
985 options = self.Options() | 952 options = self.Options() |
986 options.reset = True | 953 options.reset = True |
987 options.delete_unversioned_trees = True | 954 options.delete_unversioned_trees = True |
988 | 955 |
989 dir_path = join(self.base_path, 'dir') | 956 dir_path = join(self.base_path, 'dir') |
990 os.mkdir(dir_path) | 957 os.mkdir(dir_path) |
991 open(join(dir_path, 'nested'), 'w').writelines('new\n') | 958 open(join(dir_path, 'nested'), 'w').writelines('new\n') |
992 | 959 |
993 file_path = join(self.base_path, 'file') | 960 file_path = join(self.base_path, 'file') |
994 open(file_path, 'w').writelines('new\n') | 961 open(file_path, 'w').writelines('new\n') |
995 | 962 |
996 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 963 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
997 relpath=self.relpath) | 964 relpath=self.relpath) |
998 file_list = [] | 965 file_list = [] |
999 scm.update(options, (), file_list) | 966 scm.update(options, (), file_list) |
1000 self.assert_(not gclient_scm.os.path.isdir(dir_path)) | 967 self.assert_(not gclient_scm.os.path.isdir(dir_path)) |
1001 self.assert_(gclient_scm.os.path.isfile(file_path)) | 968 self.assert_(gclient_scm.os.path.isfile(file_path)) |
1002 self.checkstdout( | 969 sys.stdout.close() |
1003 '\n________ running \'git reset --hard HEAD\' in \'%s\'' | |
1004 '\nHEAD is now at 069c602 A and B\n' | |
1005 '\n_____ . at refs/heads/master\n' | |
1006 'Updating 069c602..a7142dc\nFast-forward\n a | 1 +\n b | 1 +\n' | |
1007 ' 2 files changed, 2 insertions(+), 0 deletions(-)\n\n' | |
1008 '\n_____ removing unversioned directory dir/\n' % join(self.root_dir, | |
1009 '.')) | |
1010 | 970 |
1011 def testUpdateUnstagedConflict(self): | 971 def testUpdateUnstagedConflict(self): |
1012 if not self.enabled: | 972 if not self.enabled: |
1013 return | 973 return |
1014 options = self.Options() | 974 options = self.Options() |
1015 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 975 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
1016 relpath=self.relpath) | 976 relpath=self.relpath) |
1017 file_path = join(self.base_path, 'b') | 977 file_path = join(self.base_path, 'b') |
1018 open(file_path, 'w').writelines('conflict\n') | 978 open(file_path, 'w').writelines('conflict\n') |
1019 try: | 979 try: |
(...skipping 19 matching lines...) Expand all Loading... |
1039 scm._Run(['commit', '-am', 'test'], options) | 999 scm._Run(['commit', '-am', 'test'], options) |
1040 __builtin__.raw_input = lambda x: 'y' | 1000 __builtin__.raw_input = lambda x: 'y' |
1041 exception = ('Conflict while rebasing this branch.\n' | 1001 exception = ('Conflict while rebasing this branch.\n' |
1042 'Fix the conflict and run gclient again.\n' | 1002 'Fix the conflict and run gclient again.\n' |
1043 'See \'man git-rebase\' for details.\n') | 1003 'See \'man git-rebase\' for details.\n') |
1044 self.assertRaisesError(exception, scm.update, options, (), []) | 1004 self.assertRaisesError(exception, scm.update, options, (), []) |
1045 exception = ('\n____ . at refs/heads/master\n' | 1005 exception = ('\n____ . at refs/heads/master\n' |
1046 '\tYou have unstaged changes.\n' | 1006 '\tYou have unstaged changes.\n' |
1047 '\tPlease commit, stash, or reset.\n') | 1007 '\tPlease commit, stash, or reset.\n') |
1048 self.assertRaisesError(exception, scm.update, options, (), []) | 1008 self.assertRaisesError(exception, scm.update, options, (), []) |
1049 # The hash always changes. Use a cheap trick. | |
1050 start = ('\n________ running \'git commit -am test\' in \'%s\'\n' | |
1051 '[new ') % join(self.root_dir, '.') | |
1052 end = ('] test\n 1 files changed, 1 insertions(+), ' | |
1053 '1 deletions(-)\n\n_____ . at refs/heads/master\n' | |
1054 'Attempting rebase onto refs/remotes/origin/master...\n') | |
1055 self.assertTrue(sys.stdout.getvalue().startswith(start)) | |
1056 self.assertTrue(sys.stdout.getvalue().endswith(end)) | |
1057 self.assertEquals(len(sys.stdout.getvalue()), | |
1058 len(start) + len(end) + 7) | |
1059 sys.stdout.close() | 1009 sys.stdout.close() |
1060 | 1010 |
1061 def testUpdateNotGit(self): | 1011 def testUpdateNotGit(self): |
1062 if not self.enabled: | 1012 if not self.enabled: |
1063 return | 1013 return |
1064 options = self.Options() | 1014 options = self.Options() |
1065 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, | 1015 scm = gclient_scm.CreateSCM(url=self.url, root_dir=self.root_dir, |
1066 relpath=self.relpath) | 1016 relpath=self.relpath) |
1067 git_path = join(self.base_path, '.git') | 1017 git_path = join(self.base_path, '.git') |
1068 rename(git_path, git_path + 'foo') | 1018 rename(git_path, git_path + 'foo') |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1225 | 1175 |
1226 if __name__ == '__main__': | 1176 if __name__ == '__main__': |
1227 if '-v' in sys.argv: | 1177 if '-v' in sys.argv: |
1228 logging.basicConfig( | 1178 logging.basicConfig( |
1229 level=logging.DEBUG, | 1179 level=logging.DEBUG, |
1230 format='%(asctime).19s %(levelname)s %(filename)s:' | 1180 format='%(asctime).19s %(levelname)s %(filename)s:' |
1231 '%(lineno)s %(message)s') | 1181 '%(lineno)s %(message)s') |
1232 unittest.main() | 1182 unittest.main() |
1233 | 1183 |
1234 # vim: ts=2:sw=2:tw=80:et: | 1184 # vim: ts=2:sw=2:tw=80:et: |
OLD | NEW |